Linq To Sql中的多个组

时间:2009-06-29 20:10:47

标签: linq-to-sql group-by

我似乎无法在互联网上找到LinqToSQl支持多个groupby的任何证据(我应该买一本书:))

我希望能够做到这一点:

      var qry = from s in DBContext.Styles
               join artist in DBContext.Artists on s.ID equals artist.StyleID
               join track in DBContext.Tracks on s.ID equals track.StyleID
               group artist by new { s.ID, s.Name } into a
               group track by new { s.ID, s.Name } into t
               select new DTO.StyleSummary
               {
                   ID = a.Key.ID,
                   Name = a.Key.Name,
                   NumberOfArtists = a.Count(),
                   NumberOfTracks = t.Count()
               };

然而第二组语句阻止编译..因为在本机sql中这是可能的,我该怎么办?

思考?这甚至可能吗?

1 个答案:

答案 0 :(得分:1)

因为你在这个LINQ语句中甚至不需要groupby。你可以做到

var qry = from s in DBContext.Styles
          join artist in DBContext.Artists on s.ID equals artist.StyleID into a
          join track in DBContext.Tracks on s.ID equals track.StyleID into t
          select new DTO.StyleSummary
          {
               ID = s.ID,
               Name = s.Name,
               NumberOfArtists = a.Count(),
               NumberOfTracks = t.Count()
          };

请注意into