Can someone help me here with some hints. Basically I would like to apply different aggregate functions (Count, Sum, Min) on different fields to the entire set (not really groupping), and return the results as fields in an anonymous type.
Something like:
var employees = new[] {
new { Name = "John Smith", Gender="M", DateOfHiring=new DateTime(2004, 10, 22), Salary=60000 },
new { Name = "Jane Smith", Gender="F", DateOfHiring=new DateTime(2008, 5, 22), Salary=55000 },
new { Name = "Kathleen Smith", Gender="F", DateOfHiring=new DateTime(2006, 10, 22), Salary=75000 },
new { Name = "David Smith", Gender="M", DateOfHiring=new DateTime(2002, 7, 12), Salary=85000 },
new { Name = "Mary Smith", Gender="F", DateOfHiring=new DateTime(2009, 6, 15), Salary=55000 }
};
var query = from e in employees
select new {
NumberOfMaleEmployees = /* Count Where Gender = 'M' */,
NumberOfFemaleEmployees = /* Count Where Gender = 'F' */,
TotalSalaries = /* Sum All */,
AverageSalary = /* Avg All */,
LatestEmployee = /* Employee with Min DateOfHiring */
};
Is it possible to achieve this in one query?
Thanks in advance
Iulian
You don’t want to be iterating over the array to start with by the looks of it – you just want:
The “latest employee” bit is slightly tricky – you basically want
MaxByfrom MoreLINQ, which would let you write:(which you can include in the above query, of course).