Lambda syntax: elements where a function has a certain value over a range of arguments
I have a custom type MyType
with a function MyBoolFunction(string)
that returns true or false.
I have a large list of MyType
objects MyTypeList
.
I have a list of string objects StringList
.
I would like to get the subset of MyTypeList
where myTypeList.MyBoolFunction(arg)
is true for at least one value of arg
as arg
ranges over StringList
.
I think I should be able to do this with C# lambda expressions.
I imagine some开发者_运维问答thing like this (pseudocode)
MyTypeList.Where(x => (x.MyBoolFunction(arg)==true for some arg in StringList);
Is this possible? How can I do this?
Try using Enumerable.Any
:
var query = MyTypeList.Where(x => StringList.Any(arg => x.MyBoolFunction(arg)));
MyTypeList.Where(x => StringList.Any(s => x.MyBoolFunction(s)));
For some clarity, s
is an entry in the StringList
and x
is an entry in MyTypeList
Without knowing your actual types i would say:
MyTypeList.Where(x => StringList.Any(arg => x.MyBoolFunction(arg));
精彩评论