gpt4 book ai didi

linq 计数错误 : DbExpressionBinding requires an input expression with a collection ResultType. 参数名称:input

转载 作者:行者123 更新时间:2023-12-02 10:52:30 27 4
gpt4 key购买 nike

我正在尝试运行以下 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com