0

I have a list of classrooms, each classroom has a list of students. How can I get a list of all the classrooms that have a student named Bob?

I tried:

var bobClassrooms = allClassrooms.SelectMany(x => x.Students)
                                 .Where(y => y.FirstName == "Bob");

But this returns me a list of students whose first name is Bob. How can I get this to be the list of classrooms?

2 Answers 2

3

Use Any:

var bobClassrooms = allClassrooms.Where(x => x.Students.Any(y => y.FirstName == "Bob"));
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, can't believe I didnt see that solution. Thanks!
2

This should work

var bobClassrooms = allClassrooms
     .Where(x => 
         x.Students.Any(y => y.FirstName == "Bob")
     );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.