2014-10-10 67 views
0

我想使用包括一些選擇這樣的關鍵字:包括與擴展方法關鍵字不提供過濾

context.Categories.Include(c => c.Products) 

我已經搜索互聯網上,發現下面的方法:

public static class ObjectQueryExtension 
    { 
     public static ObjectQuery<T> Include<T>(this ObjectQuery<T> mainQuery,Expression<Func<T, object>> subSelector) 
     { 
      return mainQuery.Include(((subSelector.Body as MemberExpression).Member as System.Reflection.PropertyInfo).Name); 
     } 
    } 

當我有複製粘貼這個方法在我的項目,當我打字

context.category.include()// 

那麼它只是顯示我用字符串作爲像參數:

context.category.include("");//like this 

我想使用包括關鍵字像這樣:

context.Categories.Include(c => c.Products) 

任何機構可以TEMM我什麼問題???

+0

只需添加「使用EntityFramework;」你的班級 – ErikEJ 2014-10-10 07:36:40

回答

0

你必須把財產作爲字符串在此擴展方法:

context.category.include("Products"); 

的dafault的System.Data.EntityInclude()也需要串路徑:

public ObjectQuery<T> Include(
    string path 
) 

你可以參考MSDN Include() docs

+0

對不起,兄弟沒有得到你 – 2014-10-10 06:29:24

+0

你可以看看這個鏈接http://tomlev2.wordpress.com/2010/10/03/entity-framework-using-include-with-lambda-expressions/ – 2014-10-10 06:29:48

+0

它會hae 2覆蓋檢查它 – 2014-10-10 06:31:25