Reputation: 1039
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
Upvotes: 4
Views: 3132
Reputation: 222
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)
};
Upvotes: 1