Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为C中的泛型列表向FindAll添加参数#_C#_Generics_Predicate_Findall - Fatal编程技术网

C# 为C中的泛型列表向FindAll添加参数#

C# 为C中的泛型列表向FindAll添加参数#,c#,generics,predicate,findall,C#,Generics,Predicate,Findall,我有一个要按整数参数过滤的对象列表 List<testObject> objectList = new List<testObject>(); // populate objectList with testObjects objectList.FindAll(GroupLevel0); private static bool GroupLevel0(testObject item) { return item._groupLevel == 0; } privat

我有一个要按整数参数过滤的对象列表

List<testObject> objectList = new List<testObject>();

// populate objectList with testObjects

objectList.FindAll(GroupLevel0);

private static bool GroupLevel0(testObject item)
{ return item._groupLevel == 0; }

private class testObject
{
     public string _FieldSQL = null;
     public int _groupLevel;
}
List objectList=new List();
//用testObjects填充objectList
objectList.FindAll(GroupLevel0);
私有静态bool GroupLevel0(testObject项)
{返回项。_groupLevel==0;}
私有类测试对象
{
公共字符串_FieldSQL=null;
公共国际组级;
}
我想做的是让GroupLevel0接受一个整数作为参数,而不是硬编码为0。我在.NET2.0中工作,所以lambda表达式是不可行的。甚至可以将参数传递到谓词中吗

谢谢,

如果您一直使用C#2.0,请使用匿名方法-只使用稍微笨重的lambda表达式(忽略表达式树):

List objectList=new List();
int desiredGroupLevel=10;
FindAll(委托(testObject项)
{
返回项。\u groupLevel==desiredGroupLevel;
});
或者您仍然可以使用方法调用开始:

List<testObject> objectList = new List<testObject>();
int desiredGroupLevel = 10;

objectList.FindAll(CheckGroupLevel(desiredGroupLevel));

...

public Predicate<testItem> CheckGroupLevel(int level)
{
    return delegate (testItem item)
    {
        return item._groupLevel == level;
    };
}
List objectList=new List();
int desiredGroupLevel=10;
FindAll(CheckGroupLevel(desiredGroupLevel));
...
公共谓词CheckGroupLevel(int级别)
{
返回委托(测试项)
{
返回项。\u groupLevel==级别;
};
}
但是,如果您使用的是Visual Studio 2008,但目标是.NET 2.0,则仍然可以使用lambda表达式。这只是一个不需要框架支持的编译器技巧(同样,忽略表达式树)

这是一个匿名委托,它关闭其父级的词法范围,因此可以看到“groupLevel”

在C#2.0及以上版本中工作。如果您将来迁移到.NET 3.5,我建议您使用lambda;
List<testObject> objectList = new List<testObject>();

// populate objectList with testObjects

objectList.FindAll(delegate(testObject o){ return GroupLevel(o, 0);} );

private static bool GroupLevel(testObject item, int groupLevel)
{ return item._groupLevel == groupLevel; }
//用testObjects填充objectList FindAll(委托(testobjecto){returngrouplevel(o,0);}); 私有静态bool GroupLevel(testObject项,int GroupLevel) {返回项。_groupLevel==groupLevel;}
此外,如果使用VS2008,在编译到2.0时仍然可以使用lambdas。它使用带有2.0目标的3.5编译器,我们已经使用了好几个月了。

您无缘无故地包装委托,只需将委托代码放入匿名委托中。我只是编辑了他的代码以用作示例。它决不是最优雅的。@Jon它不应该是
objectList=objectList.FindAll(委托(测试对象项)
??
  int groupLevel = 0;

  objectList.FindAll(
       delegate(testObject item) 
       { 
          return item._groupLevel == groupLevel; 
       });
List<testObject> objectList = new List<testObject>();

// populate objectList with testObjects

objectList.FindAll(delegate(testObject o){ return GroupLevel(o, 0);} );

private static bool GroupLevel(testObject item, int groupLevel)
{ return item._groupLevel == groupLevel; }