Linq counter error: DbExpressionBinding requires an input expression with the ResultType set. Parameter Name: Input

I am trying to run the following linq query:

var entries = from entry in _db.Entries select new CommentSummary() { NumberOfComments = entry.Message.Count(), UserName = entry.Name }; 

when I execute the request, it throws the indicated error: Message = DbExpressionBinding requires an input expression with the ResultType set. Parameter Name: Input

If i use

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

there is no error, but comments are not taken into account correctly: all NumberOfComments values ​​are "1", and must also be "1" and "0". Any ideas? Thanks

+4
source share
1 answer

you should use 'new' after 'group'. Hope this helps you.

 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) }; 
+1
source

Source: https://habr.com/ru/post/1491594/


All Articles