What does Query Projection mean in Entity Framework?

entity-framework

Question

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?

1
24
10/18/2019 2:04:35 PM

Accepted Answer

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()
         }
28
6/1/2012 10:07:22 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow