Ste*_*ald 13 linq string entity-framework include entity-framework-4
有没有替代方案:
Organizations.Include("Assets").Where(o => o.Id == id).Single()
Run Code Online (Sandbox Code Playgroud)
我希望看到类似的东西:
Organizations.Include(o => o.Assets).Where(o => o.Id == id).Single()
Run Code Online (Sandbox Code Playgroud)
避免使用硬编码字符串"Assets".
Kas*_*man 11
在EF 4.1中,有一个内置的扩展方法.
您必须在项目中引用"EntityFramework"程序集(EF 4.1所在的位置)并使用System.Data.Entity.
using System.Data.Entity;
Run Code Online (Sandbox Code Playgroud)
如果要包含嵌套实体,可以这样做:
db.Customers.Include(c => c.Orders.Select(o => o.LineItems))
Run Code Online (Sandbox Code Playgroud)
不确定这是否适用于EF4.0实体(基于ObjectContext).
Dav*_*man 10
对于Entity Framework 1.0,我为此创建了一些扩展方法.
public static class EntityFrameworkIncludeExtension
{
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> src, Expression<Func<T, StructuralObject>> fetch)
{
return src.Include(CreateFetchingStrategyDescription(fetch));
}
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> src, Expression<Func<T, RelatedEnd>> fetch)
{
return src.Include(CreateFetchingStrategyDescription(fetch));
}
public static ObjectQuery<T> Include<T, TFectchedCollection>(this ObjectQuery<T> src, Expression<Func<T, IEnumerable<TFectchedCollection>>> fetch)
{
return src.Include(CreateFetchingStrategyDescription(fetch));
}
public static ObjectQuery<T> Include<T, FetchedChild>(this ObjectQuery<T> src, Expression<Func<T, RelatedEnd>> fetch, Expression<Func<FetchedChild, Object>> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery<T> Include<T, FetchedChild>(this ObjectQuery<T> src, Expression<Func<T, RelatedEnd>> fetch, Expression<Func<FetchedChild, RelatedEnd>> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery<T> Include<T, FetchedChild>(this ObjectQuery<T> src, Expression<Func<T, RelatedEnd>> fetch, Expression<Func<FetchedChild, StructuralObject>> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery<T> Include<T, FetchedChild>(this ObjectQuery<T> src, Expression<Func<T, IEnumerable<FetchedChild>>> fetch, Expression<Func<FetchedChild, Object>> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery<T> Include<T, FetchedChild>(this ObjectQuery<T> src, Expression<Func<T, IEnumerable<FetchedChild>>> fetch, Expression<Func<FetchedChild, RelatedEnd>> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
public static ObjectQuery<T> Include<T, FetchedChild>(this ObjectQuery<T> src, Expression<Func<T, IEnumerable<FetchedChild>>> fetch, Expression<Func<FetchedChild, StructuralObject>> secondFetch)
where FetchedChild : StructuralObject
{
return src.Include(CombineFetchingStrategies(fetch, secondFetch));
}
private static String CreateFetchingStrategyDescription<TFetchEntity, TFetchResult>(
Expression<Func<TFetchEntity, TFetchResult>> fetch)
{
fetch = (Expression<Func<TFetchEntity, TFetchResult>>)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<T>(Expression<Func<T, Object>> fetch)
{
return CreateFetchingStrategyDescription<T, Object>(fetch);
}
private static String CombineFetchingStrategies<T, TFetchedEntity>(
Expression<Func<T, Object>> fetch, Expression<Func<TFetchedEntity, Object>> secondFetch)
{
return CombineFetchingStrategies<T, Object, TFetchedEntity, Object>(fetch, secondFetch);
}
private static String CombineFetchingStrategies<TFetchEntity, TFetchResult, TFetchedEntity, TSecondFetchResult>(
Expression<Func<TFetchEntity, TFetchResult>> fetch, Expression<Func<TFetchedEntity, TSecondFetchResult>> secondFetch)
{
return CreateFetchingStrategyDescription<TFetchEntity, TFetchResult>(fetch) + "." +
CreateFetchingStrategyDescription<TFetchedEntity, TSecondFetchResult>(secondFetch);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
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);
Run Code Online (Sandbox Code Playgroud)
我没有在EF4.0上测试过这个,但我希望它可以工作.
使用表达式,这很容易做到:
public static class ObjectQueryExtensions
{
public static ObjectQuery<T> Include<T, TProperty>(this ObjectQuery<T> objectQuery, Expression<Func<T, TProperty>> selector)
{
MemberExpression memberExpr = selector.Body as MemberExpression;
if (memberExpr != null)
{
return objectQuery.Include(memberExpr.Member.Name);
}
throw new ArgumentException("The expression must be a MemberExpression", "selector");
}
}
Run Code Online (Sandbox Code Playgroud)
您可以完全按照问题中的示例使用它
UPDATE
改进版本,支持多个链接属性:
public static class ObjectQueryExtensions
{
public static ObjectQuery<T> Include<T, TProperty>(this ObjectQuery<T> objectQuery, Expression<Func<T, TProperty>> selector)
{
string propertyPath = GetPropertyPath(selector);
return objectQuery.Include(propertyPath);
}
public static string GetPropertyPath<T, TProperty>(Expression<Func<T, TProperty>> selector)
{
StringBuilder sb = new StringBuilder();
MemberExpression memberExpr = selector.Body as MemberExpression;
while (memberExpr != null)
{
string name = memberExpr.Member.Name;
if (sb.Length > 0)
name = name + ".";
sb.Insert(0, name);
if (memberExpr.Expression is ParameterExpression)
return sb.ToString();
memberExpr = memberExpr.Expression as MemberExpression;
}
throw new ArgumentException("The expression must be a MemberExpression", "selector");
}
}
Run Code Online (Sandbox Code Playgroud)
示例:
var query = X.Include(x => x.Foo.Bar.Baz) // equivalent to X.Include("Foo.Bar.Baz")
Run Code Online (Sandbox Code Playgroud)