SQL to Entity Framework Count Group-By

后端 未结 5 1675
野趣味
野趣味 2020-11-28 06:26

I need to translate this SQL statement to a Linq-Entity query...

SELECT name, count(name) FROM people
GROUP by name
5条回答
  •  我在风中等你
    2020-11-28 06:58

    A useful extension is to collect the results in a Dictionary for fast lookup (e.g. in a loop):

    var resultDict = _dbContext.Projects
        .Where(p => p.Status == ProjectStatus.Active)
        .GroupBy(f => f.Country)
        .Select(g => new { country = g.Key, count = g.Count() })
        .ToDictionary(k => k.country, i => i.count);
    

    Originally found here: http://www.snippetsource.net/Snippet/140/groupby-and-count-with-ef-in-c

提交回复
热议问题