2016-07-20 7 views
-3

複数のフィールドでグループ化しようとしましたが、問題が発生しています。ピリオド、プロダクトコードでグループ化したいlinqを使用して複数のグループにグループ化する

var ProductUsageSummary = from b in myProductUsage 
          group b by b.ProductCode into g 
          select new 
          { 
           Period = g.Key, 
           Code = g.Key, 
           Count = g.Count(), 
           TotalQty = g.Sum(n => n.Qty), 
           Price = g.Average(n => n.Price) 
          }; 

もあなたが複数の列(EX ... new {prop1 prop2})上のグループへのanonymounsオブジェクトを作成することができ、およびグループ化されたフィールドはKey.PropertyName

てみによってアクセスすることができ

var ProductUsageSummary = from b in myProductUsage 
          group b by b.Period b.ProductCode into g 
          select new 
          { 
           Period = g.Key(n => n.period), 
           Code = g.Key, 
           Count = g.Count(), 
           TotalQty = g.Sum(n => n.Qty), 
           Price = g.Average(n => n.Price) 
          }; 

答えて

2

を試してみましたこの。

var ProductUsageSummary = from b in myProductUsage 
          group b by new { b.Period, b.ProductCode }into g 
          select new 
          { 
           Period= g.Key.Period, 
           Code = g.Key.ProductCode , 
           Count = g.Count(), 
           TotalQty = g.Sum(n => n.Qty), 
           Price = g.Average(n => n.Price) 
          }; 
0

これは匿名型使用して、正しい構文は次のとおりです。選択に続いて

group b by new { b.ProductCode, b.Period } into g 

を:

g.Key.ProductCodeg.Key.Period

全クエリ:

var ProductUsageSummary = from b in myProductUsage 
          group b by new { b.Period b.ProductCode } into g 
          select new 
          { 
           Period = g.Key.Period, 
           Code = g.Key.ProductCode, 
           Count = g.Count(), 
           TotalQty = g.Sum(n => n.Qty), 
           Price = g.Average(n => n.Price) 
          }; 
関連する問題