How about something like this:
IDictionary<int, int> result = context.A
.GroupBy(a => a.AId, (AId, list) => new
{
AId,
TrueCount = list.Count(i => i.Bit),
FalseCount = list.Count(i => !i.Bit)
})
.ToDictionary(x => x.AId, x => x.TrueCount - x.FalseCount)
;
Edit: Dictionaries do not preserve the order of the elements. If you want to use
OrderBy
, you'll need to use a difference collection type.
var result = context.A
.GroupBy(a => a.AId, (AId, list) => new
{
AId,
Count = list.Count(i => i.Bit) - list.Count(i => !i.Bit)
})
.OrderBy(x => x.Count)
.ToList()
;