2014-10-10 63 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