直接使用 LINQ 和 IQueryable 接口查询之间的区别?
使用实体框架 4,并给出:
ObjectSet<Thing> AllThings = Context.CreateObjectSet<Thing>;
public IQueryable<Thing> ByNameA(String name)
{
IQueryable<Thing> query = from o in AllThings
where o.Name == name
select o;
return query;
}
public IQueryable<Thing> ByNameB(String name)
{
return AllThings.Where((o) => o.Name == name);
}
两者都返回 IQueryable<>实例,因此在调用诸如 ToList()
之类的函数之前,查询不会到达服务器,对吧?是纯粹的可读性造成了差异,还是在后端使用了根本不同的技术?
Using Entity Framework 4, and given:
ObjectSet<Thing> AllThings = Context.CreateObjectSet<Thing>;
public IQueryable<Thing> ByNameA(String name)
{
IQueryable<Thing> query = from o in AllThings
where o.Name == name
select o;
return query;
}
public IQueryable<Thing> ByNameB(String name)
{
return AllThings.Where((o) => o.Name == name);
}
Both return IQueryable<> instances, and thus the query doesn't hit the server until something like ToList()
is called, right? Is it purely readability that is the difference, or are the using fundamentally different technologies on the back-end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们编译成几乎相同的代码。
第一种语法由编译器直接转换为具有第二种语法中提供的名称的方法。
这两种方法之间的主要区别实际上只是您使用不同的语法,并且分配给临时变量(查询)而不是直接返回结果。然而,出于所有实际目的,它们是相同的。
These compile to nearly identical code.
The first syntax translates directly, by the compiler, into methods with the names provided in the second syntax.
The main difference between these two methods is really just that you're using a different syntax, and that you're assigning to a temporary variable (query) instead of just returning the result directly. However, they are, for all practical purposes, identical.