You don't want to be iterating over the array to start with by the looks of it - you just want: var query = new { NumberOfMaleEmployees = employees. Count(x => x. Gender == "M"), NumberOfFemaleEmployees = employees.
Count(x => x. Gender == "F"), TotalSalaries = employees. Sum(x => x.
Salary), AverageSalary = employees. Average(x => x. Average), } The "latest employee" bit is slightly tricky - you basically want MaxBy from MoreLINQ which would let you write: LatestEmployee = employees.
MaxBy(x => x. DateOfring) (which you can include in the above query, of course).
You don't want to be iterating over the array to start with by the looks of it - you just want: var query = new { NumberOfMaleEmployees = employees. Count(x => x. Gender == "M"), NumberOfFemaleEmployees = employees.
Count(x => x. Gender == "F"), TotalSalaries = employees. Sum(x => x.
Salary), AverageSalary = employees. Average(x => x. Average), }; The "latest employee" bit is slightly tricky - you basically want MaxBy from MoreLINQ, which would let you write: LatestEmployee = employees.
MaxBy(x => x. DateOfring) (which you can include in the above query, of course).
Anyway I marked yours as an answer. And I am still curious if it's possible or not the way I put it in my initial question. – Iulian Sep 20 at 18:40 @Iulian: Yes, this iterates over the array 5 times.
If you don't want to do that, you'll have a hard time doing it elegantly in LINQ - it would be better to leave LINQ to one side and use dlev in that situation. Reactive Extensions can help to some extent, but it's got its own trickiness. – Jon Skeet Sep 20 at 18:46.
While Mr. Skeet's answer works, it does cause employees to be iterated 5 times (including the "latest employee". ) You're probably better off with a straight for loop that tracks each aggregation of interest: int maleCount = 0; int femaleCount = 0; long totalSalary = 0; double averageSalary = 0.0; object latestEmployee = null; for (int I = 0; I latestEmployee. ReDate) { latestEmployee = employeesi; } } femaleCount = employees.
Length - maleCount; averageSalary = totalSalary / (double)employees. Length; I've assumed that employees can be either male or female; you'd need to adjust your code if that assumption isn't warranted.
The example I used was just for the sake of making my question clearer, in my real case I have to get this results from an EF entity collection. – Iulian Sep 20 at 18:32 1 If you need to do multiple aggregations with an in-memory datasource, then you either need to use straight loops, or write your own extension method that hides the fact you're using a straight loop. From an EF data source, it's a little different, since the EF LINQ provider can generate SQL that captures the multiple aggregations.
At that point, the database can perform the aggregation as it sees fit. – dlev Sep 20 at 18:39 That makes sense.Thanks. – Iulian Sep 20 at 18:43.
For queries that compute a scalar value based on a collection, the Aggregate operator works over the collection. Aggregate functions appear most often in combination with partitioning the source collection. For example, we can group all countries by whether they are tropical and then aggregate the count of each group.
To do so, the aggregate operators can be used in conjunction with the Group By and Group Join clauses. Given this helper function, we use exactly the same aggregation as above, but first partition the input collection of Country and Capital pairs into groups for which Country. IsTropical is the same.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.