4 回答

TA貢獻1884條經(jīng)驗 獲得超4個贊
如果我明白你想要什么。有很多方法可以做到這一點,但是您可以先進行過濾,然后WorldEvents使用過濾后的列表重新創(chuàng)建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將反映在原始數(shù)據(jù)中。如果您不希望這樣,則需要重新創(chuàng)建每個Presentation

TA貢獻1829條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超4個贊
不確定我的理解是否正確,我猜您的 WorldEvent 和演示表之間存在一對多的關(guān)系。因此,如果您想通過使用 EntityFramework 獲取在英國發(fā)生的所有 WorldEvents 及其相關(guān)演示,您可以嘗試以下操作:
worldEventContext
.Include(PresentationContext)
.Select(
w => new
{
w.ID,
w.Name,
PresentationList = w.PresentationContext.Where(p => p.Country == "UK")
})
- 4 回答
- 0 關(guān)注
- 158 瀏覽
添加回答
舉報