2012-06-29 34 views
27

Tôi đang sử dụng khuôn khổ tổ chức và tôi không thể tìm thấy bao gồm phương pháp như trong ví dụ này:Tôi không thể tìm thấy phương thức "Bao gồm" với biểu thức lambda trong khung Entity?

using(ArticleExtractorEntities db=new ArticleExtractorEntities()) 
{ 
    Preference pref= db.Preferences.Include( 

đây tôi thấy chỉ có chức năng bao gồm với tham số (đường dẫn string) và tôi không tìm thấy bất kỳ tình trạng quá tải khác vậy làm thế nào tôi có thể sử dụng Bao gồm với biểu thức lambda?

+1

Bạn cần phải sử dụng EF 4 ** 1 ** hoặc mới hơn. Lưu ý rằng những gì tàu với VS 2010, là EF 4, mà * không * có điều này. – AakashM

Trả lời

76

không có trong System.Linq. Thêm

using System.Data.Entity 
+0

Tôi đã thêm điều này nhưng nó không hoạt động –

+1

@ahmadhori bạn đang ở trong LINQ để thực thể, hoặc LINQ để sql? –

+0

Nó phải được đánh dấu là câu trả lời được chấp nhận. –

1

Bạn có thể thực hiện nó as shown in this blog post:

public static class ObjectQueryExtension 
{ 
    public static ObjectQuery<T> Include<T>(this ObjectQuery<T> mainQuery, Expression<Func<T, object>> subSelector) 
    { 
     return mainQuery.Include(FuncToString(subSelector.Body)); 
    } 
    private static string FuncToString(Expression selector) 
    { 
     switch (selector.NodeType) 
     { 
      case ExpressionType.MemberAccess: 
       return ((selector as MemberExpression).Member as Reflection.PropertyInfo).Name; 
      case ExpressionType.Call: 
       var method = selector as MethodCallExpression; 
       return FuncToString(method.Arguments[0]) + "." + FuncToString(method.Arguments[1]); 
      case ExpressionType.Quote: 
       return FuncToString(((selector as UnaryExpression).Operand as LambdaExpression).Body); 
     } 
     throw new InvalidOperationException(); 
    } 
    public static K Include<T, K>(this EntityCollection<T> mainQuery, Expression<Func<T, object>> subSelector) 
     where T : EntityObject, IEntityWithRelationships 
     where K : class 
    { 
     return null; 
    } 
    public static K Include<T, K>(this T mainQuery, Expression<Func<T, object>> subSelector) 
     where T : EntityObject 
     where K : class 
    { 
     return null; 
    } 
} 
6

Update. Đối với những người tìm kiếm nó như thế nào mở rộng truy vấn LINQ của bạn với .Include()

Không còn là nó:

using System.Data.Entity; 

Với netcoreapp1.0 nó là:

using Microsoft.EntityFrameworkCore; 
Các vấn đề liên quan