I'm trying to solve a problem similar to the one described here
Initializing strongly typed objects in LINQ to Entities
only from totally the opposite direction. I have a number of functions in my repository, all of which return identically shaped data. The issue is my projection code:
select new pocoClass
{
// complex projection that is several pages long includes grabbing a graph of data
}
at the moment it exists for each query in the repository. I'd tried moving it into an object initializer, but that gives me the dreaded "Only parameterless constructors and initializers are supported in LINQ to Entities." issue.
I did try splitting into two queries
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);
but here final is always null and the code in new pocoClass is never called. I've included the let x & y in the above because these always vary between each use of the projection.
So, do I have to go back to multiple copies of my projection or is there another way out of this ?
The AsNoTracking() extension method returns a new query and the returned entities will not be cached by the context (DbContext or Object Context). This means that the Entity Framework does not perform any additional processing or storage of the entities that are returned by the query.
No tracking queries are useful when the results are used in a read-only scenario. They're quicker to execute because there's no need to set up the change tracking information. If you don't need to update the entities retrieved from the database, then a no-tracking query should be used.
Entity Framework Projection A projection is just a way of mapping one set of properties to another. In Entity Framework, it's a way of translating a full entity (database table) into a C# class with a subset of those properties.
I'm not sure it this is usable for you, but what I often do is create projection methods that take an IQueryable
and return an IQueryable
to translate from a domain object to a DTO. They look much like this:
public static IQueryable<CustomerDTO> ToCustomerDTO(
IQueryable<Customer> customers)
{
return
from customer in customers
select new CustomerDTO()
{
...
};
}
This allows me to have this projection in a single place. From several places in my business layer I call such a method.
There are a few things to note, though:
IQueryable
. In that case I return an array of DTOs.I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With