LINQ query should not include your class projection to improve performance

Possibly save 1 hour of your time: I have found that performance of your LINQ query could be impact especially for large datasets when you project your results directly to a class object. It is better to project to .NET object after the sql query results came back first.

var results = (from a in context.TableA
                    join b in context.TableB on a.ID equals b.ID
                    select new YourClassName
                    {
                        Year = a.Year,
                        Name = b.Name
                    });
var list = results.ToList();

The code above is much slower than the code below for large datasets.

var results = (from a in context.TableA
                    join b in context.TableB on a.ID equals b.ID
                    select new
                    {
                        Year = a.Year,
                        Name = b.Name
                    });
var list = results.ToList();

var returnList = list.Select(x => new YourClassName
                {
                    Year = x.Year,
                    Name = x.Name
                }).ToList();

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s