创建一个方法,该方法将返回将在 lambda WHERE 子句中使用的 Lambda 表达式。出现错误 1025

发布于 2024-09-28 16:04:09 字数 936 浏览 0 评论 0 原文

我在我的项目中使用框架实体 4。 我想创建一个返回表达式的函数 似乎没什么作用。我收到此内部 .Net Framework 数据提供程序错误 1025。

这是我的表达式方法,

   public Expression<Func<SupplierTypeText, bool>> GetLmbLang()
   {
       return (p => p.LangID == 1);
   }

我调用 GetLmbLang 方法并收到错误。

var ViewModel = _db.Suppliers.Select(model => new {
                model,
                SupType = model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang().Compile())
            });

            Response.Write("<pre>");
            Response.Write(((ObjectQuery)ViewModel).ToTraceString());
            Response.Write("</pre>");

如果我直接把表达式写在WHERE子句中,就没有问题。

var ViewModel = _db.Suppliers.Select(model => new {
                model,
                SupType = model.SupplierType.SupplierTypeTexts.Where(p => p.LangID == 1)
            });

谢谢。

I use Framework Entity 4 in my project.
I would like to create a function that will return an Expression
Nothing seem to work. I get this Internal .Net Framework Data Provider error 1025.

Here is my Expression Method

   public Expression<Func<SupplierTypeText, bool>> GetLmbLang()
   {
       return (p => p.LangID == 1);
   }

I call the GetLmbLang method and get an error.

var ViewModel = _db.Suppliers.Select(model => new {
                model,
                SupType = model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang().Compile())
            });

            Response.Write("<pre>");
            Response.Write(((ObjectQuery)ViewModel).ToTraceString());
            Response.Write("</pre>");

If I write the expression directly in the WHERE clause , there is no problem.

var ViewModel = _db.Suppliers.Select(model => new {
                model,
                SupType = model.SupplierType.SupplierTypeTexts.Where(p => p.LangID == 1)
            });

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦纸 2024-10-05 16:04:09

这是因为您在表达式上调用 编译方法 并且不需要到。您应该将 repBase.GetLmbLang() 作为谓词传递给Where 方法,如下所示:

SupType = model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())

编辑:

我认为您对何时应该使用 Expression> 以及何时仅使用 Func 存在一些误解。

基本上,当您在 model.SupplierType.SupplierTypeTexts 上调用 Where 方法时,您正在调用 Enumerable.Where 具有此签名的方法 (LINQ to Objects):

public static IEnumerable<TSource> Where<TSource>(
        this IEnumerable<TSource> source,
        Func<TSource, bool> predicate
)

但是,通过提供 Expression> ; 你实际上的意思是Queryable.Where< /a> 具有此签名(LINQ to Entities):

public static IQueryable<TSource> Where(
        this IQueryable<TSource> source,
        Expression<Func<TSource, bool>> predicate
)

现在发生的事情是,当您编码 model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())
SupplierTypeTexts 通过延迟加载从数据库填充,并成为内存中可供您查询的现成 EntityCollection,因此您的第一个解决方案是更改 GetLmbLang() > 相应的方法:

public Func<SupplierTypeText, bool> GetLmbLang() {
       return (p => p.LangID == 1);
}

然后在匿名类中像这样调用它:

SupType = model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())

您的第二个解决方案将是保持GetLmbLang()方法不变,并实际改变方式您创建SupType

var ViewModel = _db.Suppliers.Select(model => new {
                model,
                SupType = _db.SupplierTypeTexts
                             .Where(repBase.GetLmbLang())
            });

关于提供商错误 1205 的说明

仅当您使用匿名类型进行投影并在内部传递Expression>(如下面的代码所示)时,才会发生此异常:

var ViewModel = _db.Suppliers.Select(model => new {
        SupType = _db.SupplierTypeTexts.Where(repBase.GetLmbLang())
});

如果删除使用匿名类型的投影,或者如果您保留投影并删除返回 Expression> 的方法,它将消失。为什么会出现这种情况?我不知道,这对我来说听起来像是一个提供商的错误。但可以肯定的是,理论上它应该可以工作,并且这段代码绝对没有任何问题。话虽这么说,请随意提出一个新问题,看看其他人怎么说。

如果您想这样做,那么您的问题标题应该是这样的:
当我在传递表达式>时尝试使用匿名类型进行投影时,为什么会出现提供程序错误 1205 异常?

That's because you invoke Compile Method on your expression and you don't need to. You should just pass repBase.GetLmbLang() as the predicate to the Where method like this:

SupType = model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())

EDIT:

I think you've misunderstood a little about when you should use Expression<Func<SupplierTypeText, bool>> and when only Func<SupplierTypeText, bool>.

Basically when you invoke Where method on model.SupplierType.SupplierTypeTexts you are calling Enumerable.Where Method with this signature (LINQ to Objects):

public static IEnumerable<TSource> Where<TSource>(
        this IEnumerable<TSource> source,
        Func<TSource, bool> predicate
)

But, by providing Expression<Func<SupplierTypeText, bool>> you actually meant Queryable.Where which has this signature (LINQ to Entities):

public static IQueryable<TSource> Where(
        this IQueryable<TSource> source,
        Expression<Func<TSource, bool>> predicate
)

Now what happened there is that when you code model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())
SupplierTypeTexts is populating from database by lazy loading and become a ready EntityCollection in memory for you to query so your first solution is to change the GetLmbLang() method accordingly:

public Func<SupplierTypeText, bool> GetLmbLang() {
       return (p => p.LangID == 1);
}

and then call it like this inside your anonymous class:

SupType = model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())

Your second solution would be to keep the GetLmbLang() method as it is now and actually change the way you create SupType:

var ViewModel = _db.Suppliers.Select(model => new {
                model,
                SupType = _db.SupplierTypeTexts
                             .Where(repBase.GetLmbLang())
            });

A Note on Provider Error 1205:

This exception happens only when you are doing a projection with anonymous types and passing an Expression<Func<...>> inside like the code below:

var ViewModel = _db.Suppliers.Select(model => new {
        SupType = _db.SupplierTypeTexts.Where(repBase.GetLmbLang())
});

If you remove the projection with anonymous type or if you keep the projection and get rid of the method that returns Expression<Func<...>> it will go away. Why this happens? I have no idea, it sounds like a provider bug to me. But one thing for sure is that theoretically it should works and there is absolutely nothing wrong with this code. That being said, feel free to start a new question and see what other guys have to say.

If you want to do so, then your question title should be something like this:
Why am I getting Provider Error 1205 exception when I try to do a projection with anonymous type while passing in an Expression<Func<...>>?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文