将 LINQ 转换为 .NET 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();
}
我在 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;
}
How should I use this in .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();
}
I use this in a 3.5 projects, but now I'm adding new functionality to an old project that I cannot (at this time) upgrade to 3.5.
I just did this:
[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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您仍然可以使用 C# 3.0 但不能使用 .NET 3.5 吗? 如果是这样,请保持代码不变并使用 LINQBridge,即 LINQ to Objects为 .NET 2.0 实现。
否则,请执行以下操作:
Can you still use C# 3.0 but not .NET 3.5? If so, keep the code as it is and use LINQBridge, which is LINQ to Objects implemented for .NET 2.0.
Otherwise, do this:
也许是这样的?
Something like this perhaps?
想想该语句正在做什么,它会迭代 OperatorFields 属性中的每个项目,检查类型,然后将项目添加到列表中,然后返回完整的列表。
所以你有
Think about what the statement is doing, it's iterating over each item in the OperatorFields property, checking the type and then adding the item to a list before returning the completed list.
So you've got