4 回答
TA贡献1884条经验 获得超4个赞
如果我明白你想要什么。有很多方法可以做到这一点,但是您可以先进行过滤,然后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,所以您对 aPresentation中所做的任何更改都result将反映在原始数据中。如果您不希望这样,则需要重新创建每个Presentation
TA贡献1829条经验 获得超7个赞
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();
TA贡献1853条经验 获得超18个赞
public static List<WorldEvent> Filter(string Country, List<WorldEvent> events) {
var evs = from ev in events.Where(x => x.PresentationList.Any(y => y.Country == Country))
let targetPres = from pres in ev.PresentationList
where pres.Country == Country
select pres
select new WorldEvent {
ID = ev.ID,
Name = ev.Name,
PresentationList = targetPres.ToList()
};
return evs.ToList();
}
TA贡献1797条经验 获得超4个赞
不确定我的理解是否正确,我猜您的 WorldEvent 和演示表之间存在一对多的关系。因此,如果您想通过使用 EntityFramework 获取在英国发生的所有 WorldEvents 及其相关演示,您可以尝试以下操作:
worldEventContext
.Include(PresentationContext)
.Select(
w => new
{
w.ID,
w.Name,
PresentationList = w.PresentationContext.Where(p => p.Country == "UK")
})
- 4 回答
- 0 关注
- 112 浏览
添加回答
举报