What does Query Projection mean in Entity Framework?

前端 未结 1 1360
滥情空心
滥情空心 2020-12-06 09:22

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

相关标签:
1条回答
  • 2020-12-06 10:07

    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()
             }
    
    0 讨论(0)
提交回复
热议问题