gpt4 book ai didi

c# - DbExpressionBinding 需要一个带有集合 ResultType 的输入表达式

转载 作者:行者123 更新时间:2023-11-30 16:10:32 27 4
gpt4 key购买 nike

我想按列对表进行分组并获取计数,然后使用结果创建字典。最后一条语句返回错误

DbExpressionBinding requires an input expression with a collection ResultType.

错误是什么意思?

var a = context.Table;
var b = a.GroupBy(x => x.RecordType, (k, cnt) => new { RecType = k, cnt = k.Count() });
var c = b.Select(x =>
new KeyValuePair<string, Tuple<Type, int>>(
x.RecType, Tuple.Create(ObjTypes[x.RecType], x.cnt)))
.ToDictionary(x => x.Key, x => x.Value);

最佳答案

你的 GroupBy调用有点错误 - 它忽略了 cnt范围。应该这样写:

var b = a.GroupBy(x => x.RecordType, (k, cnt) => new { RecType = k, cnt = cnt.Count() });

该异常消息是我见过的最无用的消息之一。问题是当你调用 CountRecordType (别名为 k )在 C# 表达式中编译得很好,因为 string工具 IEnumerable<char> .不幸的是,当 EF 尝试将该表达式转换为 SQL 时,它需要能够使用 SQL count运算符,不能在 string 上工作- 它需要对一系列行进行操作。这会导致您收到有点神秘的异常。

顺便说一下,分配给 c 的查询也不会工作 - Tuple.Create不能翻译成 SQL,ObjTypes[x.RecType] 也不能.您需要调用 ToList关于变量 b在您对结果进行所有其他调整之前。

关于c# - DbExpressionBinding 需要一个带有集合 ResultType 的输入表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25653037/

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