mik*_*keM 5 c# linq lambda contains
我想过滤一个列表 FindAll
如果我写:
.FindAll(
p => p.Field == Value &&
p.otherObjList.Contains(otherObj));
Run Code Online (Sandbox Code Playgroud)
没关系,但是如果我写的话
.FindAll(
p => p.Field == Value &&
p.otherObjList.Contains(
q => q.Field1 == Value1 &&
q.Field2 == Value2));
Run Code Online (Sandbox Code Playgroud)
我得到C#语法错误消息:未知方法FindAll(?)的... otherObjList
我无法准确定义otherObj,因为我只知道两个字段Field1和Field2的值.
我做错了什么?在这种情况下我该怎么办?
Contains()大多数集合类型以及LINQ版本的方法都需要与集合相同类型的参数,而不是lambda.
您似乎只是想检查是否有任何项目符合某些条件.你应该使用这种Any()方法.
.FindAll(p => p.Field == Value
&& p.otherObjList.Any(q => q.Field1 == Value1 && q.Field2 == Value2))
Run Code Online (Sandbox Code Playgroud)