Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need LINQ expression to filter table based on foreign key values

Tags:

c#

linq

I have a table of WorldEvents. Each WorldEvent has a list of Presentations, that happened in some country, regarding that WorldEvent

public class WorldEvent
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<Presentation> PresentationList { get; set; }
}

public class Presentation
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}

public class WorldEventService
{
    public List<WorldEvent> GetWorldEvents()
    {
        List<WorldEvent> worldEventList = new List<WorldEvent>();
        List<Presentation> presentationList = new List<Presentation>();

        // Create list of Presentations for WorldEvent_1
        presentationList = new List<Presentation>()
        {
            new Presentation() { ID = 1, Name = "Presentation_1", Country = "Germany",},
            new Presentation() { ID = 2, Name = "Presentation_2", Country = "UK",},
            new Presentation() { ID = 3, Name = "Presentation_3", Country = "UK",},
        };

        // Add WorldEvent_1 to the list of WorldEvents
        worldEventList.Add(new WorldEvent()
        {
            ID = 1,
            Name = "WorldEvent_1",
            PresentationList = presentationList,
        });

        // Create list of Presentations for WorldEvent_2
        presentationList = new List<Presentation>()
        {
            new Presentation() { ID = 4, Name = "Presentation_4", Country = "USA",},
            new Presentation() { ID = 5, Name = "Presentation_5", Country = "UK",},
            new Presentation() { ID = 6, Name = "Presentation_6", Country = "Japan",},
        };

        // Add WorldEvent_2 to the list of WorldEvents
        worldEventList.Add(new WorldEvent()
        {
            ID = 2,
            Name = "WorldEvent_2",
            PresentationList = presentationList,
        });

        // Create list of Presentations for WorldEvent_3
        presentationList = new List<Presentation>()
        {
            new Presentation() { ID = 7, Name = "Presentation_7", Country = "France",},
            new Presentation() { ID = 8, Name = "Presentation_8", Country = "Germany",},
            new Presentation() { ID = 9, Name = "Presentation_9", Country = "Japan",},
        };

        // Add WorldEvent_3 to the list of WorldEvents
        worldEventList.Add(new WorldEvent()
        {
            ID = 3,
            Name = "WorldEvent_3",
            PresentationList = presentationList,
        });

        return worldEventList;
    }
}

Now - how can I get a list of WorldEvents, whose Presentations took place in the UK. And - in the list of my interest, WorldEvents should contain info about those UK Presentations only. In other word, I need this as result:

  1. WorldEvent_1(Presentation_2, Presentation_3)
  2. WorldEvent_2(Presentation_5)
like image 978
Alexey Titov Avatar asked Mar 05 '26 00:03

Alexey Titov


2 Answers

If I've understood what you want. There are many ways to do this, however you can filter first, then recreate your WorldEvents with the filtered list of Presentation

var country = "UK";

var result = worldEventList.Where(x => x.PresentationList.Any(y => y.Country == country))
                           .Select(x => new WorldEvent()
                               {
                                  ID = x.ID,
                                  Name = x.Name,
                                  PresentationList = x.PresentationList
                                                      .Where(y => y.Country == country)
                                                      .ToList()
                                }).ToList();

or as noted by Gert Arnold in the comments you could filter after the fact

var result = worldEventList.Select(x => new WorldEvent()
                 {
                     ID = x.ID,
                     Name = x.Name,
                     PresentationList = x.PresentationList
                                         .Where(y => y.Country == country).ToList()
                 }).Where(x => x.PresentationList.Any())
                   .ToList();

Note : Because this is not projecting (selecting) each Presentation, any change you make to a Presentation in the result will be reflected in the original data. If you don't want this, you will need to recreate each Presentation

like image 164
TheGeneral Avatar answered Mar 07 '26 14:03

TheGeneral


var worldEvent = new WorldEventService.GetWorldEvents();

var filter = "";//userInput

var filteredResult = worldEvent.Select(r => new WorldEvent
                     { 
                         PresentationList = r.PresentationList.Where(c => c.Country == filter).ToList(),
                         ID = r.Id,
                         Name = r.Name 
                     }).ToList();
like image 38
Gabriel Llorico Avatar answered Mar 07 '26 13:03

Gabriel Llorico