linq count error: DbExpressionBinding requires an input expression with a collection ResultType. Parameter name: input

I'm 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 query, it throws the mentioned error: Message=DbExpressionBinding requires an input expression with a collection ResultType. 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 the Comments are not counted correctly: all NumberOfComments values are "1", and there should be some "1" and some "0". Any ideas? Thanks

Answers


you should use 'by new' after 'group'. I hope this will help 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)
    };

Need Your Help

Uploading a zip file in python flask without form

python-2.7 flask zipfile

I'm trying to upload a zip file to my server using python flask request and then unzip it using zipfile module.

Stop background music

ios iphone objective-c media-player avfoundation

I have this app that has several view controllers. In the app delegate, I set it so that as soon as the app finishes launching, the background music starts. However, on another view controller, I h...