开发者

Reusing queries and the include method

开发者 https://www.devze.com 2023-03-13 16:39 出处:网络
We have a complicated LINQ to entities query and for redundancy reasons we would like to reuse the query.

We have a complicated LINQ to entities query and for redundancy reasons we would like to reuse the query.

An example to illustrate our problem:

var query = from x in context.Building.Include("Kitchen")
          select x;

In another use case, we want to eager load the "Bedroom"

var query = from x in context.Building.Include("Bedroom")
          select x;

Is it possible to add or change the Include stuff later? I am looking for something like this:

var query = from x in context.Building select x;
query.AddInclude("Kitchen");
query.AddIncldue("Ot开发者_开发百科her stuff");

How do you do query reuse?


Either download EF 4.1 and use its strongly typed extensions to IQueryable or try to write AddInclude like:

public static IQueryable<T> AddInclude(this IQueryable<T> query, string include)
{
    if (String.IsNullOrEmpty(include))
    {
        throw new ArgumetNullException("include");
    }

    var objectQuery = query as ObjectQuery<T>;

    if (objectQuery == null)
    {
        throw new InvalidOperationException();
    }

    return objectQuery.Include(include);
}  
0

精彩评论

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