gpt4 book ai didi

c# - 需要 LINQ 表达式根据外键值过滤表

转载 作者:行者123 更新时间:2023-11-30 14:44:46 24 4
gpt4 key购买 nike

我有一个世界事件表。每个 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(Presentation_5)

最佳答案

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

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

关于c# - 需要 LINQ 表达式根据外键值过滤表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57551804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com