ak1
ak1

Reputation: 379

optimization or alternate of linq (for aggreation) C#

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

Answers (3)

Mike Stockdale
Mike Stockdale

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

Marc Gravell
Marc Gravell

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:

  • what TSQL it is generating
  • how many rows it is returning
  • how long it is taking to execute at the database
  • how long it is taking to execute at the client

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

Selman Genç
Selman Genç

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

Related Questions