Making linq avoid using in memory filtering where possible

Posted by linqmonkey on Stack Overflow See other posts from Stack Overflow or by linqmonkey
Published on 2010-03-03T17:40:40Z Indexed on 2010/04/04 9:23 UTC
Read the original article Hit count: 154

Consider the these two LINQ2SQL data retrieval methods. The first creates a 'proper' SQL statement that filters the data, but requires passing the data context into the method.

The second has a nicer syntax but loads the entire list of that accounts projects, then does in memory filtering.

Is there any way to preserve the syntax of the second method but with the performance advantage of the first?

public partial class Account
{
    public IQueryable<Project> GetProjectsByYear(LinqDataContext context, int year)
    {
        return context.Projects.Where(p => p.AccountID==this.AccountID &&  p.Year==year).OrderBy(p => p.ProjectNo)
    }

    public IQueryable<Project> GetProjectsByYear(int year)
    {
        return this.Projects.Where(p => p.Year==year).OrderBy(p => p.ProjectNo).AsQueryable()
    }
}

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about Performance