DbExpressionBinding requires an input expression with a collection ResultType

别等时光非礼了梦想. 提交于 2019-12-12 15:10:13

问题


I want to group the table by a column and get the counts, then create dictionary using the result. The last statement returns the error of

DbExpressionBinding requires an input expression with a collection ResultType.

What the error means?

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);

回答1:


Your GroupBy call is slightly wrong - it's ignoring the cnt parameter. It should be written like this:

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

That exception message is one of the more unhelpful I've seen. The problem is that when you call Count on RecordType (aliased as k) in the C# expression it compiles fine, because string implements IEnumerable<char>. Unfortunately, when EF tries to convert that expression to SQL it needs to be able to use the SQL count operator, which can't work on a string - it needs to operate over a sequence of rows. This results in the somewhat cryptic exception you received.

Incidentally, the query assigned to c won't work either - Tuple.Create can't be translated into SQL, nor can ObjTypes[x.RecType]. You'll need to call ToList on variable b before you can do all that additional shaping of your results.



来源:https://stackoverflow.com/questions/25653037/dbexpressionbinding-requires-an-input-expression-with-a-collection-resulttype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!