假设您有一组Foo类:
class Foo
{
public string Bar;
public string Baz;
}
List<Foo> foolist;
Run Code Online (Sandbox Code Playgroud)
并且您想要检查此集合以查看是否有其他条目匹配Bar.
bool isDuplicate = false;
foreach (Foo f in foolist)
{
if (f.Bar == SomeBar)
{
isDuplicate = true;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Contains() 不起作用,因为它将类比较为整体.
有没有人有更好的方法来做这个适用于.NET 2.0?
Jam*_*ran 10
fooList.Exists(item => item.Bar == SomeBar)
Run Code Online (Sandbox Code Playgroud)
这不是LINQ,而是Lambda表达式,但是,它使用了v3.5功能.没问题:
fooList.Exists(delegate(Foo Item) { return item.Bar == SomeBar});
Run Code Online (Sandbox Code Playgroud)
这应该在2.0中工作.
| 归档时间: |
|
| 查看次数: |
971 次 |
| 最近记录: |