Why 'Lambda' is not supported in my LINQ to Entities syntax [duplicate]

穿精又带淫゛_ 提交于 2019-12-24 02:43:04

问题


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,
        };

Error Message:

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.


回答1:


The following should work:

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

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.




回答2:


This expression

(p, q) => SelectSearchColumns

is an expression that returns an expression. Try changing it to SelectSearchColumns on its own.




回答3:


In the expression (p, q) => SelectSearchColumns you are passing SelectSearchColumns to the Join method instead of calling it. The query provider then fails to translate it into an SQL statement, because it is passed as a data, rather than being interpreted as a call.

You need either

.Join(this.Context.CmnAddressStates, 
     p => p.StateID, 
     q => q.StateID, 
     (p, q) => SelectSearchColumns(p, q))

or the short version

.Join(this.Context.CmnAddressStates, 
     p => p.StateID, 
     q => q.StateID, 
     SelectSearchColumns)


来源:https://stackoverflow.com/questions/14852122/why-lambda-is-not-supported-in-my-linq-to-entities-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!