There are several issues with your code, or remarks to be made:
The result of the Select()
is an IEnumerable of an anonymous type with only the properties EmployeeName and Age. Therefore you will not be able to do a grouping on the property Department since that property will not exist on the anonymous type.
For the Count()
, this will be the same: IsActive doesn't exist, so it would throw the same error.
Secondly, if you were to include these properties in the anonymous type in your Select()
method, after you do the GroupBy, the IsActive wouldn't exist directly on the result of the GroupBy()
method. The GroupBy()
returns a list of groupings, where each grouping contains a key and a sequence of elements.
Now let's say all this code would work, this raises another question: why do you have the Count()
in the end?
Because by calling the Count()
method, you won't have a collection of employees anymore, just an integer holding the number of elements that satisfy the condition.