Sunday, 15 March 2015

c# - Combine select clauses in linq to entities with anonymous types -


How can I add selected streams to organizations in the links to project in an anonymous form?
Let me assume that I have these entities:

  address of public category {public string city {receive; Set; } Get Public At Zipcode {}; Set; } // other properties} public class person {public string name {get; Set; } Public Address Address {get; Set; } / / A lot of other qualities} // Employees with special characteristics to increase the person class: the public class employee: person {public double pay; Set; } Public Public Manager {get; Set; }}   

Sometimes I only have to request some qualities of my person class:

  Context.Persons .Where (.. ..) Choose (P => New {p.Name, PersonCity = p.Address.City, // other essential properties});   

And I must also request the same qualities of the specific qualities of my employees as well as:

  context Employees of Type & Lieutenant; Employee & gt; (). where (...). Select (e => New {e.Salary, ManagerName = e.Manager.Name, e.Name, PersonCity = e.City.Name, // Similar properties of previous selection with other required person's entity}};   

Is it possible with the manipulation of tree-tree ( or other solutions ) that not to duplicate all selected streams of two persons from my person unit Can you add two selected clauses?

Something like this:

  var personSelect = p = & gt; New {p.Name, PersonCity = p.Address.City, // other essential properties}; Var EmployeesHeck = PersonSock. Combine (E => New {E. Salary, Managername = AD Administrator.name}); Context.Employees.OfType & LT; Employee & gt; () Where (...) Select (employeeSelect). FirstOrDefault () .; // An anonymous item // //name = "Joaquim", // PersonCity = "Lyon", //    

No There is no way to do what you are asking. The problem is that each anonymous type has to be compiled at compile time, but the expression trees work on runtime.

I can see two ways of working around it:

  1. Anonymized type PersonData for your employee Code> is a name that contains an anonymous type with information from person .
  2. You create common types like PersonData and employee data (from the PersonData ) . Each type will be able to give you an expression to make it and the expression of employee data will be calculated based on the expression of PersonData .

    In both cases, you need some expression tubes, but it should not be difficult to do so.

No comments:

Post a Comment