2

鉴于下面的数据,我正在尝试编写一个 LINQ 语句,该语句将按 分组,如果组中有多个项目,则ParentProductId选择最大值。EndDate

由于下面列出的数据有两个相同的项目ParentProductId,我希望返回三个记录(并且对于ParentProductId = 1组使用的“2020”日期,而不是“2019”)。但是,我拥有的 LINQ 语句仍然返回所有四条记录。我究竟做错了什么?

数据:

Subscription.Add(new Subscription() { CustomerId = 555, ParentProductId = 37 , EndDate= null});
Subscription.Add(new Subscription() { CustomerId = 555, ParentProductId = 38 , EndDate = null });
Subscription.Add(new Subscription() { CustomerId = 555, ParentProductId = 1  , EndDate = new DateTime(2019, 11, 28) });
Subscription.Add(new Subscription() { CustomerId = 555, ParentProductId = 1, EndDate = new DateTime(2020, 1, 28) });

LINQ 声明:

var IndividualSubscription = (from s in db.Subscriptions
                              join ptp in db.ProductToProducts on s.ProductToProductId equals ptp.ProductToProductId
                              join p in db.Products on ptp.ParentProductId equals p.ProductId
                              where SubscriptionIds.Contains(s.OriginalSubscriptionId)
                              && s.CustomerId == CustomerId
                              group new  {ptp.ParentProductId, s.EndDate }
                              by new 
                              {
                              s.CustomerId,
                              ptp.ParentProductId,
                              p.Name,
                              s.EndDate,
                              } into grp
                              select new NCCN.Model.IndividualGroupSubscription
                                {
                                    CustomerId = grp.Key.CustomerId,
                                    ParentProductId = grp.Key.ParentProductId,
                                    ParentProductName = grp.Key.Name,
                                    EndDate = grp.Max(p => p.EndDate),
                                }).ToList();
4

2 回答 2

0

你为{ptp.ParentProductId, s.EndDate }.

如果您需要 3 行结果,则仅为 ParentProductId 分组。

var subscription = new List<Subscription>(){new Subscription() { CustomerId = 555, ParentProductId = 37 , EndDate= null}
    ,new Subscription() { CustomerId = 555, ParentProductId = 38 , EndDate = null }
    ,new Subscription() { CustomerId = 555, ParentProductId = 1  , EndDate = new DateTime(2019, 11, 28) }
    ,new Subscription() { CustomerId = 555, ParentProductId = 1, EndDate = new DateTime(2020, 1, 28) }
};

var query = subscription
    .GroupBy(x=> x.ParentProductId)
    .Select(x=> new {x.Key, EndDate = x.Max(s=>s.EndDate)});

在此处输入图像描述

于 2020-10-28T15:00:48.923 回答
0

请注意为什么您的 linq 过于矫枉过正,但遵循您的书面要求而不是您的代码,您需要的比您编写的要短一百万倍

// get grouped by product id
var result = Subscription.GroupBy(s => s.ParentProductId) 

// select to output something for each group. What you want is
// order descending all elements and pick the first which is the highest

                         .Select(grp => grp.OrderByDescending(o => o.EndDate).FirstOrDefault())

// returned as a list

                         .ToList();
于 2020-10-28T13:47:56.373 回答