I expect a very simple solution, but I can't for the life of me figure this out...
I am trying to create the LINQ equivalent of this:
SELECT Group, COUNT(*) as GroupCount
FROM table1
WHERE Valid > 0
GROUP BY Group, Project
I have this so far:
var model = _db.table1
.Where(r => r.Valid > 0)
.GroupBy(r => new { r.GROUP, r.Project})
.Select(r => new{ r.GROUP, GroupCount = r.count()};
What is wrong with my query? Visual studio throws and error stating that:
System.Linq.IGrouping' does not contain a definition for 'GROUP' and no extension method 'GROUP' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)
EDIT: Just a note: The above error is given for r.Group
in the Select
clause.
It also throws an error stating that the count
extension method doesn't exist, but I've seen other examples done this way. Maybe I was looking at an example from an old version of LINQ?
EDIT2: Some example data
GroupName ProjectID Step Other Info...
-----------------------------------------------
GroupA | 1 | 1 | ..............
GroupA | 1 | 2 |..............
GroupA | 3 | 1 | ..............
GroupB | 4 | 1 | ..............
GroupB | 5 | 1 | ..............
GroupC | 6 | 1 |..............
Desired result:
GroupName Group Count
---------------------------
GroupA | 2
GroupB | 2
GroupC | 1