C# 带有语句体的 lambda 表达式无法在 nopCommerce 中转换为表达式树

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18095665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:15:43  来源:igfitidea点击:

A lambda expression with a statement body cannot be converted to an expression tree in nopCommerce

c#asp.net-mvc-3linqentity-framework-4nopcommerce

提问by Ragesh S

I try to create a linq join query in the nopCommerce 3.0. i join two table in linq and write

我尝试在 nopCommerce 3.0 中创建一个 linq 连接查询。我在 linq 中加入两个表并写

the code successfully. but the visual studio intellicence shows the error like

代码成功。但是视觉工作室的智能显示了这样的错误

A lambda expression with a statement body cannot be converted to an expression tree

带有语句体的 lambda 表达式无法转换为表达式树

please see my code below

请看下面我的代码

 var roles = _customerEventRoleRepository.Table.Where(c => c.EventId == selevent)
                   .Join
                   (
                      _customerRepository.Table,
                      cev => cev.CustomerId, c => c.Id,
                      (cev, c) =>
                      {                             
                          var cust = new CustomerEventRolesModel();

                          cust.Id = cev.Id;
                          cust.CustomerId = c.Id;
                          cust.Customer = c.Email;
                          cust.ContactName = c.GetAttribute<string>(SystemCustomerAttributeNames.FirstName);
                          cust.CompanyName = c.GetAttribute<string>(SystemCustomerAttributeNames.Company);
                          cust.Speaker = cev.IsSpeaker;
                          cust.Sponsor = cev.IsSponser;

                          return cust;
                      }
                    ).OrderBy(cev => cev.Customer).ToList();

but the error shows

但错误显示

enter image description here

在此处输入图片说明

please help

请帮忙

采纳答案by jason

The error message is exactlywhat it says. You have a lambda expression. It has a statement body. A lambda expression with a statement body can not be converted to an expression tree. But Joinrequires an expression tree to use with EF. You should try replacing what you have with a lambda expression that doesn't have a body like:

错误消息正是它所说的。你有一个 lambda 表达式。它有一个声明主体。带有语句体的 lambda 表达式不能转换为表达式树。但是Join需要一个表达式树来与 EF 一起使用。您应该尝试用一个没有正文的 lambda 表达式替换您拥有的内容,例如:

(cev, c) => new CustomerEventRolesModel {
                Id = cev.Id,
                CustomerId = c.Id
            }

And so on.

等等。

By the way,

顺便一提,

ContactName = c.GetAttribute<string>(SystemCustomerAttributeNames.FirstName)

will NOT work with EF. Period. You better figure something else out.

不会与 EF 一起使用。时期。你最好想办法。