2010-07-02 162 views
4

喜有反正我通過i.Number,我寫的財產以後這樣LINQ集團通過兩條語句

  var r = from i in myList 
       group i by i.Number 
        into grp 
        select new 
        { 
         Reported = grp.Select(x => x.CurrentStatus).First(), 
         Number = grp.Key, 
         Sum = grp.Sum(x => x.Details[0].Quantity), 
         Name = grp.Select(x => x.Name).First(), 
         Details = grp.Select(x => x.Details).First(), 
         Descriptions = grp.Select(x => x.Descriptions).First(), 
         AssignmentId = grp.Select(x => x.AssignmentId).First(), 
         Listor = grp.Select(x => x.Number).Count() 
        }; 

這個

  var r = from i in myList 
       group i by i.Number, i.CurrentStatus 
        into grp 
        select new 
        { 
         Reported = grp.Select(x => x.CurrentStatus).First(), 
         Number = grp.Key, 
         Sum = grp.Sum(x => x.Details[0].Quantity), 
         Name = grp.Select(x => x.Name).First(), 
         Details = grp.Select(x => x.Details).First(), 
         Descriptions = grp.Select(x => x.Descriptions).First(), 
         AssignmentId = grp.Select(x => x.AssignmentId).First(), 
         Listor = grp.Select(x => x.Number).Count() 
        }; 

,這樣我可以按「組。 CurrentStatus」

回答

10

集團由一個匿名類型:

 var r = from i in myList 
      group i by new { i.Number, i.CurrentStatus } 
       into grp 
       select new 
       { 
        Reported = grp.Key.CurrentStatus, 
        Number = grp.Key.Number, 
        Sum = grp.Sum(x => x.Details[0].Quantity), 
        Name = grp.Select(x => x.Name).First(), 
        Details = grp.Select(x => x.Details).First(), 
        Descriptions = grp.Select(x => x.Descriptions).First(), 
        AssignmentId = grp.Select(x => x.AssignmentId).First(), 
        Listor = grp.Select(x => x.Number).Count() 
       }; 
+1

使用'Reported = grp.Key.CurrentStatus'而不是'Reported = grp.Select(x => x.CurrentStatus).First()'。 – 2010-07-02 13:38:35

+0

@Jaroslav:好點!我用你的修復更新了答案。 – Quartermeister 2010-07-02 14:00:09

+0

非常感謝它的作品! – Tan 2010-07-05 13:18:11