我在这个论坛上阅读了很多 group by 回复,但我还是要问:
var errandQuery = (from t in db.TimereportSet
group t by new { t.Errand.Name, t.Date } into g
select new ErrandTime { Date = g.Key.Date, Value = g.Sum(e => e.Hours) }).ToList();
为什么这不起作用。我收到以下异常:“‘字段列表’中存在未知列‘GroupBy1.K1’”
异常来自 mySQLClient。
最佳答案
您没有在 g 中选择“小时”,因此它们不会进行求和。
我不知道您的数据是什么样的,但请尝试以下操作:
编辑:
var errandQuery = (from t in db.TimereportSet
group t by new { t.Errand.Name, t.Date } into g
select new ErrandTime { Date = g.Key.Date, Value = g.Sum**(t => t.Hours)** }).ToList();
抱歉,我的第一 react 不正确。
您的 LINQ 查询是正确的,除了最后 --- 您正在使用 e..,您需要引用您选择的项目...因此您需要在 lambda 表达式中使用 t电子
关于c# - LINQ 组不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5034797/