How can I create an expression in another expression?

Forgive me if this has already been asked. I just started using LINQ. I have the following expression:

public static Expression<Func<TblCustomer, CustomerSummary>> SelectToSummary() { return m => (new CustomerSummary() { ID = m.ID, CustomerName = m.CustomerName, LastSalesContact = // This is a Person entity, no idea how to create it }); } 

I want to be able to populate LastSalesContact , which is a Person object.

The details I want to fill come from m.LatestPerson , so how can I match the fields from m.LatestPerson to LastSalesContact . I want the mapping to be reused, i.e. I do not want to do this:

 LastSalesContact = new Person() { // Etc } 

Is it possible to use a static expression, for example:

 public static Expression<Func<TblUser, User>> SelectToUser() { return x => (new User() { // Populate }); } 

UPDATE:

This is what I need to do:

 return m => (new CustomerSummary() { ID = m.ID, CustomerName = m.CustomerName, LastSalesContact = new Person() { PersonId = m.LatestPerson.PersonId, PersonName = m.LatestPerson.PersonName, Company = new Company() { CompanyId = m.LatestPerson.Company.CompanyId, etc } } }); 

But I will reuse the creation of Person() about 10-15 different classes, so I donโ€™t want the exact same code to be duplicated X times. I would also like to do the same for Company .

+4
source share
3 answers

Can you use automapper for this?

 public static Expression<Func<TblCustomer, CustomerSummary>> SelectToSummary() { return m => Mapper.Map<TblCustomer, CustommerSummary>(m); } 

You will need to do some loading, but then it will be reused.

UPDATE:

Maybe I am not getting something, but what is the purpose of this function? If you just want to map one or a collection of Tbl objects to other objects, why an expression?

You might have something like this:

 var customers = _customerRepository.GetAll(); // returns IEnumerable<TblCustomer> var summaries = Mapper.Map<IEnumerable<TblCustomer>, IEnumerable<CustomerSummary>>(customers); 

Or something I missed?

+1
source

I donโ€™t think you can use a lambda expression for this ... you will need to create an expression tree manually using the factory in Expression . Honestly, this is unlikely to be enjoyable.

My usually preferred way to develop ways to create expression trees is to start with a simple example of what you want to write as a lambda expression and then decompile it. This should show you how the expression tree is built, although the C # compiler gets the ability to use metadata related to properties more easily than we can (we should use Type.GetProperty ).

This always assumes that I understood you correctly ... it is entirely possible that I did not.

0
source

How about this:

 public static Person CreatePerson(TblPerson data) { // ... } public static Expression<Func<TblPerson, Person>> CreatePersonExpression() { return d => CreatePerson(d); } return m => (new CustomerSummary() { ID = m.ID, CustomerName = m.CustomerName, LastSalesContact = CreatePerson(m.LatestPerson) }); 
0
source

All Articles