为了账号安全,请及时绑定邮箱和手机立即绑定

需要 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 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信