代码之家  ›  专栏  ›  技术社区  ›  Jimmy

将func作为单个func返回列表

c#
  •  -1
  • Jimmy  · 技术社区  · 8 年前

    我有一个功能列表

    class SomeClass
    {
      List<Func<int, bool>> _funcList;
    
    }
    

    我想在类中编写一个函数,将列表中的所有项作为单个func返回

    Func<int, bool> GetAllTrueConditions()
    {
      //for each item in _funcList do AND and return
    }
    

    所以我可以在下面做

    Collection.Filter = obj.GetAllTrueConditions();
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Iqon    8 年前

    _funcList.All(func => func(5));
    

    var callAll = integerVal => _funcList.All(func => func(integerVal));
    

    class SomeClass
    {
        List<Func<int, bool>> _funcList;
    
        Func<int, bool> GetAllTrueConditions()
        {
            //for each item in _funcList do AND and return
            return integerVal => _funcList.All(func => func(integerVal));
        }
    }