什么是大不了的IQueryable的? [英] What is the big deal with IQueryable?

查看:189
本文介绍了什么是大不了的IQueryable的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多人在谈论的IQueryable ,我还没有完全回升上的方方面面左右。我总是用普通的列表的工作,发现他们都非常丰富的方式,你可以查询他们,并与他们合作,甚至可以运行对他们的LINQ查询。

I've seen a lot of people talking about IQueryable and I haven't quite picked up on what all the buzz is about. I always work with generic List's and find they are very rich in the way you can "query" them and work with them, even run LINQ queries against them.

我想知道如果有一个很好的理由,开始考虑在我的项目不同的默认集合。

I'm wondering if there is a good reason to start considering a different default collection in my projects.

推荐答案

的IQueryable 的界面允许您定义的查询部分针对远程LINQ提供程序(通常反对数据库,但不必须是)的多个步骤,并与延迟执行

The IQueryable interface allows you to define parts of a query against a remote LINQ provider (typically against a database, but doesn't have to be) in multiple steps, and with deferred execution.

例如。 条款;通过添加。凡(X ....... X =&GT) - 你的数据库层可以定义一些限制(例如,无论根据权限,安全性)您的查询。但是,这没有得到执行,只是还没有 - 例如你的的检索150'000行相匹配的条件的。

E.g. your database layer could define some restriction (e.g. based on permissions, security - whatever) by adding a .Where(x => x.......) clause to your query. But this doesn't get executed just yet - e.g. you're not retrieving 150'000 rows that match that criteria.

相反,你通过了的IQueryable 界面到新的水平,业务层,在那里你可能会增加额外的要求和where子句您的查询 - 再次,没有什么被执行,只是还没有,你也没有折腾出你检索到150'000行80,000 - 你只是附加的定义查询条件

Instead, you pass up the IQueryable interface to the next level, the business layer, where you might be adding additional requirements and where clauses to your query - again, nothing gets executed just yet, you're also not tossing out 80'000 of your 150'000 rows you retrieved - you're just defining additional query criteria.

和UI层可能会做同样的事情,例如基于在表格什么的用户输入。

And the UI layer might do the same thing, e.g. based on user input in a form or something.

神奇的是,你通过了的IQueryable 接口,通过所有的层,增加额外的critieria它 - 但它并没有得到实际执行/评估,直到你迫使它。这也意味着你没有选择不必要和检索吨,而最终你以后丢弃的数据。

The magic is that you're passing the IQueryable interface through all the layers, adding additional critieria to it - but it doesn't get executed / evaluated until you actually force it. This also means you're not needlessly selecting and retrieving tons of data which you end up discarding afterwards.

您不能真正做到这一点与经典的静态列表 - 你必须选择的数据,可能再次在这个过程后来放弃了很多 - 你有一个静态列表,毕竟。

You can't really do that with a classic static list - you have to pick the data, possibly discarding a lot of it again later on in the process - you have a static list, after all.

这篇关于什么是大不了的IQueryable的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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