为什么“拉姆达”没有在我的LINQ支持实体语法 [英] Why 'Lambda' is not supported in my LINQ to Entities syntax

查看:97
本文介绍了为什么“拉姆达”没有在我的LINQ支持实体语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在LINQ to Entities中运行以下查询时,会出现错误:

I get an error when I try to run the following query in LINQ to Entities:

public IEnumerable TestOne2()
{
    var query = this.Context.CmnAddressCities
        .Join(this.Context.CmnAddressStates, 
              p => p.StateID, q => q.StateID, 
              (p, q) => SelectSearchColumns)
        .ToList();

     return query;
}

public Expression<Func<CmnAddressCity,CmnAddressState, dynamic>>
    SelectSearchColumns = (e,r) => new
        {
            CityID = e.CityID,
            CityName = e.CityName,
            StateName=r.StateName,
        };

错误讯息


在LINQ to Entities中不支持LINQ表达式节点类型Lambda。

The LINQ expression node type 'Lambda' is not supported in LINQ to Entities.

想要知道为什么会出现这个错误,如何解决这个问题。

Want to know why this error arise,how to solve this.

如果有任何疑问请问,谢谢先进。

If have any query please ask ,thanks in advanced.

推荐答案

以下应该工作:

var query = this.Context.CmnAddressCities
                .Join(this.Context.CmnAddressStates, 
                      p => p.StateID, 
                      q => q.StateID, 
                      SelectSearchColumns)
                .ToList();

查看第二行到最后一行。您的版本创建一个返回表达式的表达式。 EF提供程序不能将其转换为SQL,SQL中的表达式的等价是什么?这不存在。

您只需将参数本身传递给表达式。

See the second to last line for the difference. Your version creates an expression that returns an expression. The EF provider can't translate that to SQL, what is the equivalence of an expression in SQL? That doesn't exist.
You simply have to pass your expression as the parameter itself.

这篇关于为什么“拉姆达”没有在我的LINQ支持实体语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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