2014-02-14 71 views
1

我很努力讓groupby在LINQ to SQL中工作,我對它很新穎......我試圖groupby g.id,但它不會工作......任何幫助都會不勝感激...乾杯GroupBy LINQ to SQL不起作用,或許

  IQueryable<GuestList> query = from t in _ttx.Trips 
      join l in _ttx.Legs on t.Id equals l.TripId 
      join gl in _ttx.GuestLegs on l.Id equals gl.LegId 
      join g in _ttx.Guests on gl.GuestId equals g.Id 
      where t.Id == id 

      select new GuestList() 
      { 
       Id = g.Id, 
       Name = g.Name, 
       NoOfLegs = g.GuestLegs.Count() 
      }; 

我的結果是

1 paul 3 
2 Jim 1 
1 paul 3 
1 paul 3 

回答

1

請嘗試如下所示。

var query2 = (from ps in query 
       group ps by new { ps.Id } into prod 
       select new GuestList 
       { 
        Id = prod.Key.Id, 
        Name = prod.Name, 
        NoOfLegs = prod.Sum(c => c.NoOfLegs), 
        }).OrderBy(x => x.Id).ToList(); 
+0

嗨Sampath,非常感謝您的回覆。它引導我回答:) – user3024760