gurehbgui
gurehbgui

Reputation: 14694

how to create a many to many linq query in c#?

i have a problem with a linq statement, i have a structure which looks like this:

Class1 has a id and a reference to a list of Class2 Class2 has a id and a reference to a list of Class1

now i want to make a linq query like this: get all of Class1 where the id == 1 from these elements get all which dont have a Class2 with the id == 2 (in the reference list)

how to do this with one command?

Upvotes: 0

Views: 59

Answers (1)

ColinE
ColinE

Reputation: 70160

How about the following ...

List<Class1> classOneList = ...
List<Class2> classTwoList = ...

var items = classOneList.Where(c1 => c1.Id == 1)
                        .Where(c1 => !c1.Class2Collection.Any(c2 => c2.Id == 2));

Upvotes: 4

Related Questions