Is there any alternative to this:
Organizations.Include(\"Assets\").Where(o => o.Id == id).Single()
I would like to see something like:<
For Entity Framework 1.0, I created some extensions methods for doing this.
public static class EntityFrameworkIncludeExtension
{
public static ObjectQuery Include(this ObjectQuery src, Expression> fetch)
{
return src.Include(CreateFetchingStrategyDescription(fetch));
}
public static ObjectQuery Include(this ObjectQuery src, Expression> fetch)
{
return src.Include(CreateFetchingStrategyDescription(fetch));
}
public static ObjectQuery Include(this ObjectQuery src, Expression>> fetch)
{
return src.Include(CreateFetchingStrategyDescription(fetch));
}
public static ObjectQuery Include(this ObjectQuery src, Expression> fetch, Expression> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery Include(this ObjectQuery src, Expression> fetch, Expression> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery Include(this ObjectQuery src, Expression> fetch, Expression> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery Include(this ObjectQuery src, Expression>> fetch, Expression> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery Include(this ObjectQuery src, Expression>> fetch, Expression> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery Include(this ObjectQuery src, Expression>> fetch, Expression> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
private static String CreateFetchingStrategyDescription(
Expression> fetch)
{
fetch = (Expression>)FixedWrappedMemberAcces.ForExpression(fetch);
if (fetch.Parameters.Count > 1)
throw new ArgumentException("CreateFetchingStrategyDescription support only " +
"one parameter in a dynamic expression!");
int dot = fetch.Body.ToString().IndexOf(".") + 1;
return fetch.Body.ToString().Remove(0, dot);
}
private static String CreateFetchingStrategyDescription(Expression> fetch)
{
return CreateFetchingStrategyDescription(fetch);
}
private static String CombineFetchingStrategies(
Expression> fetch, Expression> secondFetch)
{
return CombineFetchingStrategies(fetch, secondFetch);
}
private static String CombineFetchingStrategies(
Expression> fetch, Expression> secondFetch)
{
return CreateFetchingStrategyDescription(fetch) + "." +
CreateFetchingStrategyDescription(secondFetch);
}
}
Usage:
Orders.Include(o => o.Product); // generates .Include("Product")
Orders.Include(o => o.Product.Category); // generates .Include("Product.Category")
Orders.Include(o => o.History); // a 1-* reference => .Include("History")
// fetch all the orders, and in the orders collection.
// also include the user reference so: .Include("History.User")
// but because history is an collection you cant write o => o.History.User,
// there is an overload which accepts a second parameter to describe the fetching
// inside the collection.
Orders.Include(o => o.History, h => h.User);
I haven't tested this on EF4.0, but I expect it to work.