我在Entity框架中找不到lambda表达式的“Include”方法?

时间:2012-06-29 09:41:21

标签: c# .net entity-framework

我正在使用实体框架,我找不到像这个例子中的include方法:

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

这里我发现只有函数include with参数(字符串路径),我没有找到任何其他的重载,那么我如何使用Include with lambda expression?

4 个答案:

答案 0 :(得分:86)

它不在System.Linq中。 添加

using System.Data.Entity

答案 1 :(得分:7)

更新。对于那些看起来如何使用.Include()

扩展linq查询的人

不再是:

using System.Data.Entity;

使用netcoreapp1.0,它是:

using Microsoft.EntityFrameworkCore;

答案 2 :(得分:1)

您可以实施它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;
    }
}

答案 3 :(得分:0)