Entity Framework - LinQ projection problem?

No, you can't project onto a mapped object. You can use an anonymous type instead: var orders = (from order in context. ORDERS select new { REFERENCE = order.

REFERENCE, OPERATION = order. OPERATION }).

– mbp Dec 10 '10 at 12:07 2 You can't map this to an Entity. You can create a custom object (DTO) for this, but you can't partially load an entity, because it EF would not know what to do with it when you try to alter and safe it. – Steven Dec 10 '10 at 13:21.

The problem with the above solution is that from the moment you call AsEnumerable(), the query will get executed on the database. In most of the cases, it will be fine. But if you work with some large database, fetching the whole table(or view) is probably not what you want.So, if we remove the AsEnumerable, we are back to square 1 with the following error: The entity or complex type 'ModelContextName.

ORDERS' cannot be constructed in a LINQ to Entities query. I have been struggling with this problem for a whole day and here is what I found. I created an empty class inheriting from my entity class and performed the projection using this class.

Public sealed class ProjectedORDERS : ORDERS {} The projected query (using covariance feature): IQueryable orders = (from order in context. ORDERS select new ProjectedORDERS { REFERENCE = order. REFERENCE, OPERATION = order.

OPERATION, }); VoilÃ! You now have a projected query that will map to an entity and that will get executed only when you want to.

1 I too have spent a lot of time on this very issue and, after a thousand failed attempts, came to the same solution as you; empty class inheriting from the entity and be done with it. Still, I would love to know why EF doesn't allow projecting to entity classes, as this would come incredibly handy in a lot of circumstances :( – Daniel Liuzzi May 8 at 10:07.

I think the issue is creating new entities within the query itself, so how about trying this: context.ORDERS.ToList(). Select(o => new ORDERS { REFERENCE = o. REFERENCE, OPERATION = o.

OPERATION }).

That won't solve the issue with the SQL query. It will be the same as if you fetch the whole object and will fetch data from all fields. After that is executed, you transform the results in memory.

However, there is really no point of doing that afterwards. – Yakimych Dec 10 '10 at 17:50 Please check my first post because I can't post source code here. – mbp Dec 14 '10 at 8:58.

This can occur because the Entity Framework translates a query against a conceptual model into an equivalent query against the data source. When a single entity set in the conceptual model maps to more than one table in the data source, or when a relationship between entities is mapped to a join table, the query command executed against the data source query may require one or more joins. Use the ToTraceString method of the ObjectQuery or EntityCommand classes to view the commands that are executed against the data source for a given query.

For more information, see How to: View the Store Commands (Entity Framework). Nested Entity SQL queries may create joins on the server and can return a large number of rows. SELECT c, (SELECT c, (SELECT c FROM AdventureWorksModel.

In addition, such queries cause the query pipeline to generate a single query with duplication of objects across nested queries.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions