What does Query Projection mean in Entity Framework?
Projection is when the result of a query is output to a different type than the one queried. Another article defined it as : the process of transforming the results of a query
Projection can be to an anonymous type, but could also be to a concrete type. If you come from a SQL world, it is akin to the columns listed in your SELECT
clause.
Example selecting a sub-set of an object into an concrete type:
ParentObj.Select(x=> new ParentSlim { ParentID = x.ParentID, Name = x.Name } );
.
Example merging to object into a 3rd anonymous type:
Note: the select new
portion is the projection.
from P in ParentObj.AsQueryable()
join C in ChildObj.AsQueryable() on P.ParentID == C.ParentID
select new { // <-- look ma, i'm projecting!
ParentID = P.ParentID,
Name = P.Name,
SubName = C.Name
RandomDate = DateTime.UtcNow()
}
Related videos on Youtube
atconway
I am a Practice Lead Consultant designing and building applications in web and .NET and surrounding technologies with a concentration in the following: JavaScript/TypeScript/Angular/React, C#/VB.NET, ASP.NET Core/MVC, Web API/WCF, EF, and SQL Server placing an emphasis on OOP, Architecture, and Design. I thoroughly enjoy web and Microsoft development technologies and have been a proponent of their languages and platforms since I began work as an engineer 20+ years ago.
Updated on September 15, 2022Comments
-
atconway over 1 year
I am looking at some EF examples and trying to decipher what 'Query Projection' exactly equates to when doing LINQ to Entities or EntitySQL. I believe it is when the query results are filtered and projected into an anonymous type but not 100% sure.
Can someone please define this and maybe provide a small L2E query that uses an example of it?
-
EBarr almost 12 yearsAgreed, I enjoy that guys writing.
-
Amir over 8 years@EBarr: Query projection can improve indexing on sql server? how?
-
EBarr over 8 years@Amir - where did I say that? I do see how it could result in faster executions under certain circumstances. If, however, you have distinct question it's best to post a question and allow people to fully respond.