“LINQ 表达式节点类型“Invoke”; LINQ to Entities 不支持” - 难住了!

发布于 2024-10-21 12:04:44 字数 602 浏览 2 评论 0 原文

稍后在我的 EF 中,我尝试传入一个匿名函数作为我的 Linq 查询的一部分。该函数将传入一个 INT 并返回一个 BOOL(u.RelationTypeId 是一个 INT)。下面是我的函数的简化版本:

public IEnumerable<UserBandRelation> GetBandRelationsByUser(Func<int, bool> relation)
{
    using (var ctx = new OpenGroovesEntities())
    {
        Expression<Func<UsersBand, bool>> predicate = (u) => relation(u.RelationTypeId);

        var relations = ctx.UsersBands.Where(predicate);

        // mapping, other stuff, back to business layer
        return relations.ToList();
    }
}

但是,我收到了上述错误。看起来我通过从函数构建谓词来使一切正确。有什么想法吗?谢谢。

In my EF later, I'm trying to pass in an anonymous function to be used as part of my Linq query. The function would pass in an INT and return a BOOL (u.RelationTypeId is an INT). Below is a simplified version of my function:

public IEnumerable<UserBandRelation> GetBandRelationsByUser(Func<int, bool> relation)
{
    using (var ctx = new OpenGroovesEntities())
    {
        Expression<Func<UsersBand, bool>> predicate = (u) => relation(u.RelationTypeId);

        var relations = ctx.UsersBands.Where(predicate);

        // mapping, other stuff, back to business layer
        return relations.ToList();
    }
}

However, I get the error stated above. It seems like I'm going everything correct by building a predicate from the function. Any ideas? Thanks.

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

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

发布评论

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

评论(4

一片旧的回忆 2024-10-28 12:04:44

我遇到了这个错误,我正在使用 Entity Framework 和 Joe Albahari 的 PredicateBuilder 来构建动态 where 子句。如果您碰巧处于相同的情况,您应该调用 AsExpandable 方法:

如果使用实体框架查询,请将最后一行更改为:

返回objectContext.Products.AsExpandable().Where(谓词);

此方法是 LINQKIT DLL 的一部分,您可以获取此处 或通过 NuGet 包此处

现在一切正常。 :)

I was getting this very error and I'm using Entity Framework with PredicateBuilder by Joe Albahari to build dynamic where clauses. If you happen to be in the same condition, you should call the AsExpandable method:

If querying with Entity Framework, change the last line to this:

return objectContext.Products.AsExpandable().Where(predicate);

This method is part of LINQKIT DLL that you can grab here or through a NuGet package here.

Everything works fine now. :)

耳根太软 2024-10-28 12:04:44

您正在尝试传递任意 .NET 函数...实体框架如何希望将其转换为 SQL?您可以将其更改为采用 Expression> 来代替,并从中构建 Where 子句,尽管它不会是特别容易,因为您需要使用不同的参数表达式重写表达式(即用调用u.RelationTypeId的表达式替换原始表达式树中的任何参数表达式)。

老实说,为了在用于创建表达式树以传递到方法中的 lambda 表达式中指定 u.RelationTypeId,您最好只使用:

public IEnumerable<UserBandRelation> GetBandRelationsByUser(
    Expression<Func<UsersBand, bool>> predicate)
{
    using (var ctx = new OpenGroovesEntities())
    {
        var relations = ctx.UsersBands.Where(predicate);

        // mapping, other stuff, back to business layer
        return relations.ToList();
    }
}

You're trying to pass an arbitrary .NET function in... how could the entity framework hope to translate that into SQL? You can change it to take an Expression<Func<int, bool>> instead, and build the Where clause from that, although it won't be particularly easy, because you'll need to rewrite the expression with a different parameter expression (i.e. replacing whatever parameter expression is in the original expression tree with the expression of calling u.RelationTypeId).

To be honest, for the sake of just specifying u.RelationTypeId in the lambda expression that you use to create the expression tree to pass into the method, you'd be better off just using:

public IEnumerable<UserBandRelation> GetBandRelationsByUser(
    Expression<Func<UsersBand, bool>> predicate)
{
    using (var ctx = new OpenGroovesEntities())
    {
        var relations = ctx.UsersBands.Where(predicate);

        // mapping, other stuff, back to business layer
        return relations.ToList();
    }
}
信愁 2024-10-28 12:04:44

您可以在 Where 请求之前调用 谓词 上的 Expand() 方法。

You can call the Expand() method on your predicate before the Where request.

简单爱 2024-10-28 12:04:44

我知道这个答案确实很晚了,但我遇到了同样的问题,它引导我来到这里,所以我想我会分享我的解决方案。

我读了莱尼尔的回答,它给了我一个想法。默认类型具有“AsEnumerable()”方法,其行为方式相同,从而缓解了该问题。

I know this answer is really late, but I ran into the same problem and it led me here, so I thought I'd share my solution.

I read Leniel's answer, and it gave me an idea. The default types have the method "AsEnumerable()" which behaves the same way, alleviating the issue.

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