亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要 LINQ 表達式根據外鍵值過濾表

需要 LINQ 表達式根據外鍵值過濾表

C#
德瑪西亞99 2023-09-09 16:59:49
我有一張世界事件表。每個世界事件都有一個在某個國家/地區發生的與該世界事件相關的演示列表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",},        };現在 - 我怎樣才能獲得在英國進行演示的 WorldEvents 列表。而且 - 在我感興趣的列表中,WorldEvents 應僅包含有關英國演示文稿的信息。換句話說,我需要這個結果:世界事件_1(演示文稿_2、演示文稿_3)世界事件_2(演示_5)
查看完整描述

4 回答

?
慕村9548890

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


查看完整回答
反對 回復 2023-09-09
?
吃雞游戲

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();


查看完整回答
反對 回復 2023-09-09
?
慕容森

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();        

    }


查看完整回答
反對 回復 2023-09-09
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

不確定我的理解是否正確,我猜您的 WorldEvent 和演示表之間存在一對多的關系。因此,如果您想通過使用 EntityFramework 獲取在英國發生的所有 WorldEvents 及其相關演示,您可以嘗試以下操作:


worldEventContext

    .Include(PresentationContext)

    .Select(

        w => new

        {

            w.ID,

            w.Name,

            PresentationList = w.PresentationContext.Where(p => p.Country == "UK")

        })


查看完整回答
反對 回復 2023-09-09
  • 4 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號