作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试运行以下 linq 查询:
var entries = from entry in _db.Entries
select new CommentSummary()
{
NumberOfComments = entry.Message.Count(),
UserName = entry.Name
};
当我执行查询时,它会抛出上述错误:Message=DbExpressionBinding 需要带有集合 ResultType 的输入表达式。参数名称:输入
如果我使用
var entries = from entry in _db.Entries
group entry by entry.Name into groupedByName
orderby groupedByName.Count() descending
select new CommentSummary
{
NumberOfComments = groupedByName.Count(),
UserName = groupedByName.Key
};
没有错误,但评论计数不正确:所有 NumberOfComments 值都是“1”,应该有一些“1”和一些“0”。有任何想法吗?谢谢
最佳答案
您应该在“group”之后使用“by new”。我希望这会对您有所帮助。
var entries = from entry in _db.Entries
group entry by new { entry.Name } into groupedByName
select new
{
groupedByName.Key.Name,
NumberOfComments = groupedByName.Count(x => x.Name != null)
};
关于linq 计数错误 : DbExpressionBinding requires an input expression with a collection ResultType. 参数名称:input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17671505/
我是一名优秀的程序员,十分优秀!