开发者

Making linq avoid using in memory filtering where possible

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)
    开发者_StackOverflow中文版{
        return this.Projects.Where(p => p.Year==year).OrderBy(p => p.ProjectNo).AsQueryable()
    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜