在我的搜索操作方法中使用 IQueryable 或 IEnumerable
我有以下操作方法:-
[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Search(string q, int classid)
{
var users = r.searchusers(q, classid);
// code does here..............
调用以下模型存储库方法:-
public IQueryable<User> searchusers(string q, int id)
{
return from u in entities1.Users
where (!u.Users_Classes.Any(c => c.ClassID == id) && (u.UserID.Contains(q))
select u;
}
现在,如果我将 IQueryable 更改为 IEnumerable,如下所示,在这种情况下执行查询的方式是否会有任何更改 ?:-
public IEnumerable<User> searchusers(string q, int id)
{
return from u in entities1.Users
where (!u.Users_Classes.Any(c => c.ClassID == id) && (u.UserID.Contains(q))
select u;
}
i have the following action method:-
[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Search(string q, int classid)
{
var users = r.searchusers(q, classid);
// code does here..............
which calls the following model repository method:-
public IQueryable<User> searchusers(string q, int id)
{
return from u in entities1.Users
where (!u.Users_Classes.Any(c => c.ClassID == id) && (u.UserID.Contains(q))
select u;
}
now if i change the IQueryable to IEnumerable as follow , will there be any changes on how the query will be executed in this case ?:-
public IEnumerable<User> searchusers(string q, int id)
{
return from u in entities1.Users
where (!u.Users_Classes.Any(c => c.ClassID == id) && (u.UserID.Contains(q))
select u;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,在我的测试中,一旦转换为 IEnumerable,就确定了查询 SQL。此后执行的任何其他查询组合都将在执行查询后在内存中完成。
因此,假设您有一个加载用户列表并返回 IEnumerable 的基本查询。然后,在实际运行该列表(从而执行查询)之前,您还添加一个 .Where(i=>i.username='bob')。在这种情况下,它将执行整个选择,然后对“where username='bob'”部分应用内存过滤器中的 LINQ-to-Objects,这可能不是您想要的,而是您希望整个事情作为 SQL 语句的一部分运行。
所以,是的,只要有可能,请始终使用 IQueryable,以便立即运行完整的组合。
Yes, in my testing once you cast to IEnumerable, that determines the query SQL. Any additional query composition you do after that will be done in memory after the query is executed.
So suppose you have a base query that loads a list of users and returns an IEnumerable. Then before you actually run through that list (thereby executing the query), you also add a .Where(i=>i.username='bob'). In that case, it will execute the whole select, and then apply a LINQ-to-Objects in memory filter for the "where username='bob'" part, which is probably not what you want, instead you want the whole thing to be run as part of the SQL statement.
So yes, always use IQueryable whenever you can so that your fully composed are run at once.
是的,它会更改查询,您希望将 IQuerable 用于任何使用远程数据源的内容。
在您的情况下,它将强制 linq 执行查询,而 IQuerable 会等待其他人执行查询。如果愿意,IQuerable 允许他们附加更多条件以推送到数据库以供执行。
我通常在层边界强制使用 IEnumerable,在这些地方我不希望人们修改系统将生成的查询。
Yes it will change the query, you want to use the IQuerable for anything that uses a remote datasource.
In your case it will force linq to execute the query, where as IQuerable would wait until someone else to execute the query. IQuerable allows them, if they desire, to append more conditions to push down to the database for execution.
I generally enforce IEnumerable at the layer boundary, places where I dont want people modifying the queries that the system is going to generate.