简体   繁体   English

我在实体框架中找不到带有lambda表达式的“包含”方法吗?

[英]I can't find “Include” method with lambda expression in Entity framework?

I am using entity framework and I can't find include method like in this example: 我正在使用实体框架,但找不到此示例中的include方法:

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

here i find only the function include with the parameter (string path) and I don't find any other overload so how can I use Include with lambda expression? 在这里,我只找到包含参数(字符串路径)的函数,而没有找到其他任何重载,那么如何在lambda表达式中使用Include?

it's not in System.Linq. 它不在System.Linq中。 Add

using System.Data.Entity

Update. 更新。 For those looking how it extend your linq query with .Include() 对于那些希望通过.Include()扩展linq查询的人

No longer is it: 不再是:

using System.Data.Entity;

With netcoreapp1.0 it is: 使用netcoreapp1.0,它是:

using Microsoft.EntityFrameworkCore;

You could implement it 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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试导航到第三级实体属性时,在Include方法内找不到带有lambda表达式的Select方法 - When I try navigating to third level entity properties, I can't find Select method with lambda expression inside Include method Entity Framework Core 3.0 - Include 中使用的 Lambda 表达式无效 - Entity Framework Core 3.0 - Lambda expression used inside Include is not valid 在 Include 中使用的 Lambda 表达式在 Entity Framework Core 中无效 - Lambda expression used inside Include is not valid in Entity Framework Core 将 lambda 表达式移动到实体框架中的 static 扩展方法 - Move a lambda expression to a static extension method in Entity Framework 具有Include方法的实体框架 - Entity framework with Include method 实体框架-Lambda表达式中的NotSupportedException - Entity framework - NotSupportedException in lambda expression 如何&#39;不&#39;实体框架的lambda表达式 - how to 'not' a lambda expression for entity framework 如何优化此lambda表达式以使用包含单词列表的每一行的实体框架进行查询? - How can I optimise this lambda expression to query using entity framework for each row that contains a list of words? 实体框架:我可以包含子类型的属性吗? - Entity Framework: Can I Include() properties of subtypes? 为什么我无法在Entity Framework的SaveChanges()方法上捕获SqlException - Why I can't catch SqlException on SaveChanges() method of Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM