我正在尝试翻译以下查询:
SELECT STATE, COUNT(*)
FROM MYTABLE
GROUP BY STATE;
转换为lambda表达式。我正在使用C#和EntityFramework,但是我似乎不能让它工作。这是到目前为止我的仓库里有的东西:
public IEnumerable<object> PorcentajeState(Guid id)
{
return _context.Sates.Where(a => a.Id == id)
.GroupBy(a => a.State)
.Select(n => new { n.StateId , n.Count() });
}
当然它不能编译,我在谷歌上搜索了2个小时后迷路了。你能帮帮我吗?
提前感谢
发布于 2016-03-25 15:44:21
这个不错
public IEnumerable<object> PorcentajeState(Guid id)
{
return _context.Sates.Where(a => a.Id == id)
.GroupBy(a => a.StateId)
.Select(g => new { g.Key, Count = g.Count() });
}
但是你可以试试这个。
public IEnumerable<object> PorcentajeState(Guid id)
{
return _context.Sates.Where(a => a.Id == id)
.GroupBy(a => a.StateId)
.Select(g => new { g.Key.StateId, Count = g.Count() });
}
https://stackoverflow.com/questions/19285443
复制相似问题