"该LINQ前pression节点类型“调用”不LINQ支持实体" - 难倒! [英] "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities" - stumped!

查看:297
本文介绍了"该LINQ前pression节点类型“调用”不LINQ支持实体" - 难倒!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的EF后,我想在一个匿名函数传递给被用作我的LINQ查询的一部分。该功能将通过在一个int并返回布尔(u.RelationTypeId是INT)。下面是我的函数的简化版本:

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();
    }
}

不过,我得到上述错误。好像我会用一切从建筑功能的predicate正确的。有任何想法吗?谢谢你。

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.

推荐答案

您正试图通过在...怎么可能实体框架,希望它转换成SQL任意.NET功能?你可以改变它采取一种防爆pression&LT; Func键&LT; INT,BOOL&GT;&GT; 代替,并建立了其中,从该条款,但它不会是的尤其的容易,因为你需要使用不同的参数e pression改写前pression(如更换任何参数前pression是在调用 u.RelationTypeId )的前pression原来前pression树。

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).

说实话,对于刚刚指定 u.RelationTypeId 中拉姆达前pression着想,你用它来创建前pression树传递到方法,你会更好只使用:

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();
    }
}

这篇关于&QUOT;该LINQ前pression节点类型“调用”不LINQ支持实体&QUOT; - 难倒!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆