Selecting count in LINQ

后端 未结 4 1017
忘掉有多难
忘掉有多难 2021-01-14 00:50

I got a SQL Server table with columns ResolvedDate and ResolvedBy.

Now I want to select those two columns and count their results, which I

4条回答
  •  醉酒成梦
    2021-01-14 00:57

    If you're trying to count each item by date, you'd need to use GroupBy:

    var countsByDate = _dateContext.Activities
                               .Where(a => a.IsResolved && a.ResolvedBy == userId)
                               .GroupBy(a => a.ResolvedDate)
                               .Select(g => new {ResolvedDate = g.Key, Count = g.Count() });
    

提交回复
热议问题