How can i combine select clause in linq to entities in order to project into an anonymous type?
Assume that I have these entities:
public class Address
{
public string City { get; set; }
public int ZipCode { get; set; }
//other properties
}
public class Person
{
public string Name { get; set; }
public Address Address { get; set; }
//a LOT of other properties
}
//extend person class with employee specific properties
public class Employee : Person
{
public double Salary { get; set; }
public Person Manager { get; set; }
}
Sometimes I only need to request few properties of my Person class:
Context.Persons.Where(...).Select(p => new
{
p.Name,
PersonCity = p.Address.City,
//other needed properties
});
And I also need to request the same properties of my Employee class plus the specific properties:
Context.Employees.OfType<Employee>().Where(...).Select(e => new
{
e.Salary,
ManagerName = e.Manager.Name,
e.Name,
PersonCity = e.City.Name,
//other needed properties identical as the previous select with Person entity
});
Is it possible with expression-tree manipulations (or another solution) to combine two select clauses in order to not duplicate all the select clause from my Person entity?
Something like that:
var personSelect = p => new {
p.Name,
PersonCity = p.Address.City,
//other needed properties
};
var employeeSelect = personSelect.Combine(e => new {
e.Salary,
ManagerName = e.Manager.Name
});
context.Employees.OfType<Employee>().Where(...).Select(employeeSelect).FirstOrDefault();
// returns an anonymous object
// {
// Name = "Joachim",
// PersonCity = "Lyon",
// <other Person properties>
// Salary = 65432.10,
// ManagerName = "Samuel"
// }