2011-12-28 179 views
1

我有SQL並試圖將ti轉換爲Linq查詢。我需要通過幾個字段對記錄進行分組,Linq中如何做到這一點?Linq to SQL GroupBy幾個字段

var statistic = DataAccess.Instance.Statistics.Where(
p => 
    p.DateStamp >= minDate && 
    p.DateStamp <= DateTime.UtcNow && p.UserId == userId && 
    p.Url.Contains(url) 
    ). 
    GroupBy(p=>p.PageTitle //How to group by second(Url) field? 


SELECT PageTitle 
      , Url 
      , Count(*) As [Count] 
      FROM Statistic 
     WHERE URL like @url 
     AND DateSpan BETWEEN @dateRange1 AND @dateRange2 
     AND UserId = @UserId 
     GROUP BY PageTitle, Url 
     ORDER BY Count(*) DESC, Url; 
+0

可能重複(http://stackoverflow.com/questions/847066/group-by-多列-LINQ) – Pleun 2011-12-28 15:17:17

回答

1

您需要按一個匿名類型:

.GroupBy(p => new { p.PageTitle, p.Url }) 
[集團通過多列 - LINQ]的