查询投影在实体框架中是什么意思? [英] What does Query Projection mean in Entity Framework?

查看:50
本文介绍了查询投影在实体框架中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些EF示例,并试图解释在对实体或EntitySQL进行LINQ时,查询投影精确地等于什么。我相信是在查询结果被过滤并投影为匿名类型但不是100%确定的时候。

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.

有人可以定义一下,也许提供一个小的L2E查询来

Can someone please define this and maybe provide a small L2E query that uses an example of it?

推荐答案

投影是指查询结果输出为不同于查询类型的类型。 另一篇文章将其定义为:转换查询结果的过程

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

投影可以是匿名类型,也可以是具体类型。如果您来自SQL世界,则类似于 SELECT 子句中列出的列。

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.

示例,将对象的子集选择为具体类型:

ParentObj.Select(x=> new ParentSlim { ParentID = x.ParentID,  Name = x.Name } );



示例合并为第3个匿名类型的对象:

注意:选择新的部分是投影。

.
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()
         }

这篇关于查询投影在实体框架中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆