views:

17

answers:

1

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()
    }
}
A: 

I am not an exper so this may not be the best way, but,

you could add the LinqDataContext to your class (account), initialise it in the constructor so you function can use it without having to pass it as an argument

zsquare