在Linq Query中进行多组分组选择

时间:2014-10-16 10:34:25

标签: c# linq

在Linq查询中分组后进行多重选择

listItems = feeditem.Select(o => new 
                                { 
                                  MerchantName = o.MerchantName,
                                  ProductName= o.ProductName, 
                                  TT = o.TotalTransactions 
                                }).GroupBy(fl => fl.MerchantName).ToList()
                                  .Select(fl => new MerchantPerformanceAffiliate 
                                 { 
                                   MerchantName = fl.Key, 
                                   TotalTransactions = fl.Sum(x => x.TT)
                                 }).ToList();

1 个答案:

答案 0 :(得分:2)

试试这个: -

var query = from feed in feedItems
                         group feed by new { feed.MerchantName, feed.ProductName } into feedGroup
                         select new MerchantPerformanceAffiliate 
                         {
                             MerchantName = feedGroup.Key.MerchantName,
                             ProductName = feedGroup.Key.ProductName,
                             TotalTransactions = feedGroup.Sum(x => x.TotalTransactions)
                         };

这是工作Fiddle

这是方法语法代码(以防万一): -

 var query2 = feedItems.GroupBy(feed => new { feed.MerchantName , feed.ProductName })
                                  .Select(items => new MerchantPerformanceAffiliate 
                                                                {
                                                                    MerchantName = items.Key.MerchantName,
                                                                    ProductName = items.Key.ProductName,
                                                                    TotalTransactions = items.Sum(x => x.TotalTransactions)
                                                                }).ToList();