Retrieve a list where a string is contained inside a list inside a list

Basically I have a list of objects. Let me call them meetings. Inside the meetings there is another list of facilities. Let me name these participants. I want to return all meetings where there is a certain participant in the list.

I need something like this:

meetings.Where(meeting => meeting.Participants.Name == "Test name").ToList();

In principle, return the list of meetings where the meeting has a participant named "Test Name".

EDIT: I actually used the MongoDB filter. Before I just extract all the “collections” (with a filter) and then use LINQ to filter the list. You can also filter the results at the database level. But it's good to know.

+4
source share
3 answers

Any?

var result = meetings
  .Where(meeting => meeting
     .Participants
     .Any(participant => participant.Name == "Test name"))
  .ToList();
+6

LINQ Any :

var result = meetings.Where(m => m.Participants.Any(p => p.Name == "Test name")).ToList();
+3

Even you try the following:

 meetings.Where(m => m.Participants.Any(k => k.Name == "Test Name")).ToList();
0
source

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


All Articles