I have used two LINQ queries to manipulate a datatable and return the data that I want. Effectively, the table has the following columns:
Group Animal Numbers There can be multiple Animals within a single group, and multiple numbers assigned to a single animal. Initially I want to calculate the range of the numbers for each specific animal, and then calculate an average range for each group. With the output being the group number and then the average range.
I have the following two LINQ queries, that do work, however my question is, can this be merged into a single query, or , as they are distinct operations, should they be kept seperate for readability and testability?
//Calculate range/delta for each number per animal var query = from data in Data.AsEnumerable() group data by new { animal = data.Field<string>("animal"), gp = data.Field<string>("group") } into g orderby g.Key.gp select new { animal = g.Key.animal, gp = g.Key.gp, delta = g.Max(c => Convert.ToDouble(c.Field<string>("numbers"))) - g.Min(c => Convert.ToDouble(c.Field<string>("numbers"))) }; //calculate average range/delta per group var temp = from q in query group q by q.gp into g select new { gp = g.Key, average = g.Average(c => c.delta) }; Thanks.