Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

list

lambda

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 something like this (pseudocode)

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

Is this possible? How can I do this?

like image 420
Vivian River Avatar asked Dec 17 '22 17:12

Vivian River


1 Answers

Try using Enumerable.Any:

var query = MyTypeList.Where(x => StringList.Any(arg => x.MyBoolFunction(arg)));
like image 180
Mark Byers Avatar answered Feb 01 '23 23:02

Mark Byers