专注.NET平台

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

比如有如下的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)                     

                    }; 

当要取s中某个字段值的时候,是不能使用p.Single().SalesPerson 这种方式,这时候会发生一个异常 “sequence contains more than one element”,这时候我们可以通过这种方式取得SalesPersonName = p.Max(s=>s.SalesPerson)。

posted on 2010-01-07 10:16  吴春晖  阅读(440)  评论(0)    收藏  举报