A lambda expression with a statement body cannot be converted to an expression tree in nopCommerce [duplicate]
Asked Answered
F

1

10

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

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

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

Fetal answered 7/8, 2013 at 5:56 Comment(2)
Your lambda is a function. This function cannot be converted to SQL. You need to find another way to do what you're doing.Arum
Thank you for your valueable reply. here the ContactName and CompanyName are needed in the query result.Fetal
P
-1

The error message is exactly what 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 Join requires 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:

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

Peripeteia answered 7/8, 2013 at 6:7 Comment(3)
Thanks for your valuable reply. I am really sorry sir. I am not a experianced person. how can i include the ContactName and CompanyName in this query result. I think need to change whole function for adding ContactName and CompanyName fields.Fetal
By typing them in. Mimic what I did for the first two properties.Peripeteia
-1; repeating the content of error message in patronisingly short sentences and with added ALL CAPS is unlikely to help the asker understand why his code was illegal. In particular, this answer doesn't explain what an expression tree is, what EF uses them for, or why the compiler isn't capable of doing (or willing to do) the trivial rearrangement that makes the compilation error going away (which is extracting the lambda body out into a method and using a call to that method as the value of a body-less lambda expression).Drift

© 2022 - 2024 — McMap. All rights reserved.