Avoid repeating projection code in Entity Framework

I am trying to solve a problem similar to that described here

Initializing strongly typed objects in LINQ for objects

only from a completely opposite direction . I have a number of functions in my repository, all of which return identical data. The problem is my projection code:

select new pocoClass
 {
   // complex projection that is several pages long includes grabbing a graph of data
 }

at the moment when it exists for each request in the repository. I tried moving it to the object initializer, but it gives me the scary "LINQ to Entities only supports constructors without parameters and initializers." question.

I tried splitting two requests

var candidates = (from thing in _entities.whatever
  where (complex.stuff==true)
  select thing);

var final = (from thing in candidates.AsEnumerable()
  let x = thing.ITEMS.Where(blah=>blah.blah==param)
  let y = x.OTHERITEMS.FirstOrDefault()
  select new pocoClass(thing,x,y);

final null, pocoClass . let x y , .

, ?

+5
1

, , , , , IQueryable IQueryable DTO. :

public static IQueryable<CustomerDTO> ToCustomerDTO(
    IQueryable<Customer> customers)
{
    return
        from customer in customers
        select new CustomerDTO()
        {
           ...
        };
}

. - .

:

  • , -. - .
  • DTO, , . , IQueryable. DTO.
  • DTO. , , .

, .

+12

All Articles