4 回答
TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果我明白你想要什么。有很多方法可以做到這一點(diǎn),但是您可以先進(jìn)行過濾,然后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在評(píng)論中指出的那樣,您可以在事后過濾
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();
注意:因?yàn)檫@不是投影(選擇)每個(gè)Presentation,所以您對(duì) aPresentation中所做的任何更改都result將反映在原始數(shù)據(jù)中。如果您不希望這樣,則需要重新創(chuàng)建每個(gè)Presentation
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
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貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
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貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
不確定我的理解是否正確,我猜您的 WorldEvent 和演示表之間存在一對(duì)多的關(guān)系。因此,如果您想通過使用 EntityFramework 獲取在英國(guó)發(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)注
- 213 瀏覽
添加回答
舉報(bào)
