linq 分组

var data = from r in listRecords
                       group r by
                               new
                               {
                                   r.CampaignId,
                                   r.CityId,
                                   r.Gift_DistributorId,
                                   r.ProductGiftId,

                                   FirstQuantity = listRecords.Where(a => a.CampaignId == r.CampaignId
                                       && a.CityId == r.CityId
                                       && a.Gift_DistributorId == r.Gift_DistributorId
                                       && a.ProductGiftId == r.ProductGiftId).Sum(c => c.FirstQuantity),

                                   Premium = listRecords.Where(a => a.CampaignId == r.CampaignId
                                                            && a.CityId == r.CityId
                                                            && a.Gift_DistributorId == r.Gift_DistributorId
                                                            && a.ProductGiftId == r.ProductGiftId).Sum(c => c.Premium)
                               } into g
                       select g.Key
                          ;
参考 http:
//stackoverflow.com/questions/5231845/c-sharp-linq-group-by-on-multiple-column //修改后 var data2 = from r in listRecords group r by new { r.CampaignId, r.CityId, r.Gift_DistributorId, r.ProductGiftId, } into g select new { g.Key.CampaignId, g.Key.CityId, g.Key.Gift_DistributorId, g.Key.ProductGiftId, FirstQuantity = g.Sum(a => a.FirstQuantity), Premium = g.Sum(a => a.Premium) };

 

posted @ 2015-09-04 22:33  哈哈2222  阅读(286)  评论(0编辑  收藏  举报