I am trying to group by a column and count the number of true/false rows in other columns.
For example we have a entity class:
public class Example {
public int Id { get; set; }
public int ParentId { get; set; }
public string Type { get; set; }
public bool Vote { get; set; }
}
I need to group by Type and count the number of true/false votes where the ParentId is a certain value. What I have:
await _dbContext.Example
.Where(x => x.ParentId == parentid)
.GroupBy(x => new { t.Type, t.Vote })
.Select(x => new
{
TrueVotes = x.Count(v => v.Vote == true),
FalseVotes = x.Count(v => v.Vote == false),
Type = x.Key.Type
})
.ToListAsync();
Entity throws an exception when this code runs saying it cannot be converted to SQL. The issue is with the Count call.
Performance does need to be taken into account as well.