Lambda syntax: elements where a function has a specific value over a range of arguments

I have a custom MyType type 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 a subset of MyTypeList , where myTypeList.MyBoolFunction(arg) true for at least one arg value as arg runs through the StringList .

I think I should do this using C # lambda expressions.

I represent something like this (pseudo code)

 MyTypeList.Where(x => (x.MyBoolFunction(arg)==true for some arg in StringList); 

Is it possible? How can i do this?

+4
source share
3 answers

Try using Enumerable.Any :

 var query = MyTypeList.Where(x => StringList.Any(arg => x.MyBoolFunction(arg))); 
+8
source
 MyTypeList.Where(x => StringList.Any(s => x.MyBoolFunction(s))); 

For some clarity, s is an entry in StringList and x is an entry in MyTypeList

+3
source

Not knowing my real types, I would say:

 MyTypeList.Where(x => StringList.Any(arg => x.MyBoolFunction(arg)); 
+1
source

Source: https://habr.com/ru/post/1341212/


All Articles