Reputation: 379
I want to optimize the performance of following LINQ statement or need some alternatives which could perform in more efficient way...
var temp = (from r in jc
group r by new { r.City, r.Gender, r.AgeBracket } into g
select new Summary
{
Population = g.Sum(x => (decimal)x.Population),
State = g.Select(x => x.City).First(),
Gender = g.Select(x => x.Gender).First(),
AgeBracket = g.Select(x => x.AgeBracket).First()
}).ToArray();
Please let me know any alternative approaches of LINQ to perform such operations, because I observed that LINQ has a considerable performance overhead in producing result on large amount of data.
Edit-1 I am not querying it from database but I have custom in memory objects that I use to generate summaries.
Upvotes: 1
Views: 115
Reputation: 5266
You can do it the old-fashioned way :) Sort jc
by city, gender, age. Iterate through the sorted result summing population. When city, gender, age changes, output a summary with previous city, gender, age and sum. Reset sum to zero and continue.
Upvotes: 0
Reputation: 1062895
Firstly, LINQ isn't about performance - it is about convenience. To find out how it is performing, you need to profile. The first thing to do is to find out:
if the TSQL looks good, and the two timings are about the same, then you probably need to add indexes. If the TSQL is horrible and/or it is taking too long to run at the database, then you probably need to hand-roll the TSQL. If the database time is fine, but it is taking a lot of time to execute in .NET, then it is probably a mapper glitch (it happens sometimes) - consider tools like dapper. If it is simply the number of rows: think of a new design; perhaps paging.
There is no one answer to optimization. First you need to identify the bottleneck.
Upvotes: 2
Reputation: 101701
Instead of g.Select(xxx).First
you can use g.Key.XXX
:
select new Summary
{
Population = g.Sum(x => (decimal)x.Population),
State = g.Key.City,
Gender = g.Key.Gender,
AgeBracket = g.Key.AgeBracket
}
Upvotes: 4