首页 > 解决方案 > 需要 LINQ 表达式来根据外键值过滤表

问题描述

我有一张 WorldEvents 表。每个 WorldEvent 都有一个在某个国家/地区发生的关于该 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;
    }
}

现在 - 我怎样才能获得 WorldEvents 的列表,它们的演讲在英国举行。而且 - 在我感兴趣的列表中,WorldEvents 应该只包含有关这些英国演示文稿的信息。换句话说,我需要这个结果:

  1. WorldEvent_1(Presentation_2,Presentation_3)
  2. WorldEvent_2(演示文稿_5)

标签: c#linq

解决方案


如果我明白你想要什么。有很多方法可以做到这一点,但是您可以先过滤,然后WorldEvents使用过滤后的列表重新创建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();

或者正如Gert Arnold在评论中指出的那样,您可以在事后过滤

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();

注意:因为这不是每个都投影(选择) ,所以您对 a 所做的Presentation任何更改都将反映在原始数据中。如果你不想要这个,你需要重新创建每个PresentationresultPresentation


推荐阅读