WCF Ria、LINQ to Entity 投影、过滤、延迟执行
假设我有以下实体
User
- 用户ID
- 用户名
- 用户名为Lower
- 个人资料(导航属性)
配置文件
- 用户ID
- 电子邮件
- EmailAsLower
- 语言
,我想返回一个合并对象,我们将其称为 UserDTO,但删除一些我们不希望用户看到的字段(AsLower 内容)
- UserDTO
- 用户ID
- 用户名
- 电子邮件
- 语言
如果我按如下方式编写 LINQ 查询,然后对其应用过滤条件,由于过滤条件应用于预计的类属性名称,它会在此时执行吗?
public IQueryable<UserDTO> GetUsers()
{
IQueryable<UserDTO> users = (from u in Entities.Users select new UserDTO() { UserID = u.UserID, UserName = u.UserName, Email = u.Profile.Email, Language = u.Profile.Email });
users = users.Where( u => u.UserName.Contains("Admin") );
return users;
}
我问的原因是如果它背后的数据库表恰好很大(数百万条记录),我想确保它不会首先尝试将其加载到内存中。
Lets say I've got the following entities
User
- UserID
- UserName
- UserNameAsLower
- Profile (Navigation property)
Profile
- UserID
- EmailAsLower
- Language
and I want to return a consolidated object, lets call it a UserDTO, but remove some of the fields that we don't want the user to see (AsLower stuff)
- UserDTO
- UserID
- UserName
- Language
if I write a LINQ query as follows and then apply filtering criteria on it, will it execute at that point since the filter criteria is being applied to the projected class property names?
public IQueryable<UserDTO> GetUsers()
{
IQueryable<UserDTO> users = (from u in Entities.Users select new UserDTO() { UserID = u.UserID, UserName = u.UserName, Email = u.Profile.Email, Language = u.Profile.Email });
users = users.Where( u => u.UserName.Contains("Admin") );
return users;
}
The reason I'm asking is if the database table behind it happens to be really large (millions of records), I want to make sure it doesn't try to load it into memory first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RIA 将使用所有应用的筛选器执行 LINQ 查询,并且仅序列化回结果。
操作顺序是这样的:客户端构造 IQueryable,客户端枚举 IQueryable,RIA 将 IQueryable.Expression 转换为 GET 请求,服务器将 GET 请求解析为表达式树并重构 IQueryable,服务器使用 IQueryProvider 执行表达式,服务器序列化响应给客户端。
RIA will execute the LINQ query with all applied filters and only serialize back the results.
Order of operations is this: client constructs IQueryable, client enumerates IQueryable, RIA turns IQueryable.Expression into GET request, server parses GET request into Expression tree and reconstructs IQueryable, server executes expression using IQueryProvider, server serializes response to client.