我有SQL并尝试将ti转换为Linq查询。我需要按几个字段对记录进行分组,如何在Linq中执行此操作?
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 :(得分:1)
您需要按匿名类型进行分组:
.GroupBy(p => new { p.PageTitle, p.Url })