I feel like I am missing something simple, but I have not found the documentation that answers my question.
I have recently been decomposing some of the linq projections into reusable expressions. It works great when operating on a collection, but I can't seem to figure out how to apply an expression to a single object in another expression. Below is an example of what I am trying to accomplish:
public class Person
{
public string ID { get; set; }
public string Name { get; set; }
}
public class PersonDto
{
public string ID { get; set; }
public string Name { get; set; }
}
public class Department
{
Person Manager { get; set; }
List<Person> Employees { get; set; }
}
public class DepartmentDto
{
PersonDto Manager { get; set; }
List<PersonDto> Employees { get; set; }
}
public Expression<Func<Person, PersonDto>> CreatePersonDto = p => new PersonDto
{
ID = p.ID,
Name = p.Name
};
public Expression<Func<Department, DepartmentDto>> CreateDepartmentDto = d => new DepartmentDto
{
Manager = d.Manager // How do I transform this `Person` using `CreatePersonDto`
Employees = d.Employees.Select(CreatePersonDto) //Does not work either
};
EDIT: To be clear, I am using Linq-to-Entities that needs to use this Expression to generate a SQL statement. As a result, I cannot Compile
the expression to a Func
as I might be able to using Linq-to-Objects.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…