Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq不分组_C#_Linq - Fatal编程技术网

C# Linq不分组

C# Linq不分组,c#,linq,C#,Linq,根据下面的数据,我试图编写一个LINQ语句,该语句将按ParentProductId进行分组,如果组中有多个项,则选择最大EndDate 由于下面列出的数据有两项具有相同的ParentProductId,我希望返回三条记录(对于ParentProductId=1组使用的是“2020”日期,而不是“2019”)。然而,我的LINQ声明仍然返回所有四条记录。我做错了什么 数据: LINQ声明: var IndividualSubscription = (from s in db.Subscripti

根据下面的数据,我试图编写一个LINQ语句,该语句将按
ParentProductId
进行分组,如果组中有多个项,则选择最大
EndDate

由于下面列出的数据有两项具有相同的
ParentProductId
,我希望返回三条记录(对于
ParentProductId=1
组使用的是“2020”日期,而不是“2019”)。然而,我的LINQ声明仍然返回所有四条记录。我做错了什么

数据:

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

请注意,为什么您的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();

您为
{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)});
var subscription=new List(){new subscription(){CustomerId=555,ParentProductId=37,EndDate=null}
,新订阅(){CustomerId=555,ParentProductId=38,EndDate=null}
,新订阅(){CustomerId=555,ParentProductId=1,EndDate=new DateTime(2019,11,28)}
,新订阅(){CustomerId=555,ParentProductId=1,EndDate=new DateTime(2020,1,28)}
};
var查询=订阅
.GroupBy(x=>x.ParentProductId)
.Select(x=>new{x.Key,EndDate=x.Max(s=>s.EndDate)});

有两个项目具有相同的ParentProductId
。。。用
…EndDate=new DateTime(2019,11,28)。。。。EndDate=new DateTime(2020,1,28)
组新{ptp.ParentProductId,s.EndDate}
是否应从
组新
中删除它?是的,我可以工作,但是LINQ语句呢?@Jefferson不知道你的意思。我编写了代码,从您提供的数据中可以得到所需的准确输出。如果你需要其他的东西,那是另一个问题,或者你问错了问题,请不要在图片中张贴代码。这使得人们很难复制它供自己使用,而且在搜索引擎中找不到。对不起,我调整了它。
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)});