c# - 需要 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 应该只包含有关这些英国演示文稿的信息。换句话说,我需要这个结果:
- WorldEvent_1(Presentation_2,Presentation_3)
- WorldEvent_2(演示文稿_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();
注意:因为这不是每个都投影(选择) ,所以您对 a 所做的Presentation
任何更改都将反映在原始数据中。如果你不想要这个,你需要重新创建每个Presentation
result
Presentation
推荐阅读
- angular - 带有 window.location.href 的 Angular 7 单元测试
- python - 重复相同的数据库查询返回增量结果
- javascript - 有没有办法检查用户是否与范围输入进行了交互
- ruby-on-rails - 下拉项目的格式不正确
- php - PHP if 语句导致显示空白页 - 检查计划表
- python - 如何实现 Python 代码来解决二次规划?
- sql - SQL 累积不重复计数
- docker - 禁用谷歌云注册表的 docker config.json 凭据
- c - 如何在不存储到变量中的情况下针对多个值测试函数的返回值
- java - 如何检查继承链中是否存在特定的类类型