views:

25

answers:

1

Hi folks,

I'm trying to do some eager loading on an EF Entity.

so, if the entity is called Orders .. then I guess i would do the following...

_someContext.Orders.Include("Whatever") ....

But the problem is, I have a method like the following ...

public IQueryable<Order> Find(Expression<Func<Order, bool>> predicate)
{
    return CurrentContext.Orders.Where(predicate);
}

which works great .. but can i leverage the Expression predicate to include the Include("whatever") in there, instead of having to add another method parameter?

A: 

I don't think so. Since the predicate and ObjectQuery.Where Method in genral has nothing to do with eager loading by Include. You can create a extension method though, but that does not save you from having yet another parameter for specifying the include:

public IQueryable<Order> Find(Expression<Func> predicate, string include) {
    return CurrentContext.Orders.Where(predicate, include);
}

public static ObjectQuery<T> Where<T>(this ObjectQuery<T> entity, Expression<Func<T, bool>> predicate, string include) {
    return (ObjectQuery<T>)entity.Include(include).Where<T>(predicate);
}
Morteza Manavi
Yeah -this was what i did a few hours ago .. sorta. I didn't bother with the extension method but just went ahead and did added another string[] includeAssociations parameter to my Find(..) method(s).
Pure.Krome