Take the following LINQ query as an example. Please do not comment on the code itself, as I just typed it to help with this question.
The following LINQ query uses 'group by' and computes the resulting information. As you can see, there are numerous calculations that are performed on the data, but how efficient LINQ is behind the scenes.
var NinjasGrouped = (from ninja in Ninjas group pos by new { pos.NinjaClan, pos.NinjaRank } into con select new NinjaGroupSummary { NinjaClan = con.Key.NinjaClan, NinjaRank = con.Key.NinjaRank, NumberOfShoes = con.Sum(x => x.Shoes), MaxNinjaAge = con.Max(x => x.NinjaAge), MinNinjaAge = con.Min(x => x.NinjaAge), ComplicatedCalculation = con.Sum(x => x.NinjaGrade) != 0 ? con.Sum(x => x.NinjaRedBloodCellCount)/con.Sum(x => x.NinjaDoctorVisits) : 0, ListOfNinjas = con.ToList() }).ToList();
- How many times is the ninja list renamed to calculate each of the values?
- Is it faster to use a foreach loop to speed up the execution of such a request?
- Adding ".AsParallel ()" after Ninjas leads to any performance improvements?
- Is there a better way to calculate the summary for List?
Any advice is appreciated since we use this type of code in all of our software, and I would really like to get a better idea of ββwhat LINQ does under the hood (so to speak). Perhaps there is a better way?
source share