Linq Group By 注意点
比如有如下的Linq查詢:?
var q = from s in agedDebtCollection
?? ? ? ? ? ? ? ? ? ?group s by s.SalesPersonCode into p
?? ? ? ? ? ? ? ? ? ?select new
?? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ?Key = p.Key,
?? ? ? ? ? ? ? ? ? ? ? ?//here can not using p.Select(s=>s).Single().SalesPerson
?? ? ? ? ? ? ? ? ? ? ? ?// or p.Single().SalesPerson
?? ? ? ? ? ? ? ? ? ? ? ?SalesPersonName = p.Max(s=>s.SalesPerson),
?? ? ? ? ? ? ? ? ? ? ? ?Current = p.Sum(s => s.BucketCurrent),
?? ? ? ? ? ? ? ? ? ? ? ?TO30 = p.Sum(s => s.ZeroToThirty),
?? ? ? ? ? ? ? ? ? ? ? ?TO60 = p.Sum(s => s.ThirtyToSixty),
?? ? ? ? ? ? ? ? ? ? ? ?TO90 = p.Sum(s => s.SixtyToNinety),
?? ? ? ? ? ? ? ? ? ? ? ?TO120 = p.Sum(s => s.NinetyToHundredtwenty),
?? ? ? ? ? ? ? ? ? ? ? ?Over120 = p.Sum(s => s.OverHundredtwenty) ? ? ? ? ? ? ? ? ? ??
?? ? ? ? ? ? ? ? ? ?};?
當(dāng)要取s中某個(gè)字段值的時(shí)候,是不能使用p.Single().SalesPerson 這種方式,這時(shí)候會(huì)發(fā)生一個(gè)異常 “sequence contains more than one element”,這時(shí)候我們可以通過這種方式取得SalesPersonName = p.Max(s=>s.SalesPerson)。
轉(zhuǎn)載于:https://www.cnblogs.com/minisquirrel/archive/2010/01/07/1640972.html
總結(jié)
以上是生活随笔為你收集整理的Linq Group By 注意点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 泰拉瑞亚苔藓如何获得
 - 下一篇: 战神诸神黄昏坩埚任务如何通关