Reputation: 29209
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);
Upvotes: 3
Views: 6185
Reputation: 3905
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.
Upvotes: 4