繁体   English   中英

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

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

我正在使用实体框架,但找不到此示例中的include方法:

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

在这里,我只找到包含参数(字符串路径)的函数,而没有找到其他任何重载,那么如何在lambda表达式中使用Include?

它不在System.Linq中。

using System.Data.Entity

更新。 对于那些希望通过.Include()扩展linq查询的人

不再是:

using System.Data.Entity;

使用netcoreapp1.0,它是:

using Microsoft.EntityFrameworkCore;

您可以实现此博客文章中所示的方法

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM