将LINQ转换为.NET 2.0

bal*_*dre 3 c# linq c#-2.0

我应该如何在.NET 2.0中使用它?

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<OperatorField> FindByType(String type)
{
    //  return only selected type
    return (from ce in this.OperatorFields where ce.Type == type select ce).ToList();
}
Run Code Online (Sandbox Code Playgroud)

我在3.5个项目中使用它,但现在我正在为旧项目添加新功能,我不能(此时)升级到3.5.


我这样做了:

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<OperatorField> FindByType(String type)
{
    //  return only selected type
    //return (from ce in this.OperatorFields where ce.Type == type select ce).ToList();

    List<OperatorField> r = new List<OperatorField>();

    foreach (OperatorField f in this.OperatorFields)
        if (f.Type == type)
            r.Add(f);

    return r;
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 12

你还能使用C#3.0而不是.NET 3.5吗?如果是这样,请保持代码不变并使用LINQBridge,它是为.NET 2.0实现的LINQ to Objects.

否则,这样做:

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<OperatorField> FindByType(String type)
{
    List<OperatorField> list = new List<OperatorField>();
    foreach (OperatorField ce in OperatorFields)
    {
        if (ce.Type == type)
        {
            list.Add(ce);
        }
    }
    return list;
}
Run Code Online (Sandbox Code Playgroud)

  • @balexandre:不,你误会我了.您可以使用Visual Studio 2008和C#3.0功能,但仍然以.NET 2.0为目标.您不需要您的客户端升级框架以使其工作. (5认同)