使用IQueryable的使用LINQ [英] Using IQueryable with Linq

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

问题描述

什么是LINQ的?

它是用于开发扩展方法或任何其他目的?

Is it used for developing extension methods or any other purpose?

推荐答案

马克Gravell的答案是很完整的,但我想我会但从用户的角度补充一下这个,还有...

Marc Gravell's answer is very complete, but I thought I'd add something about this from the user's point of view, as well...

的主要区别,从用户的角度来看,是这样的,当你使用的IQueryable< T> (用一个能够正确支持的事情提供商),你可以节省很多资源

The main difference, from a user's perspective, is that, when you use IQueryable<T> (with a provider that supports things correctly), you can save a lot of resources.

例如,如果你对一个远程数据库,很多ORM系统的工作,你有两种方式从表中读取数据的选项,其中一个返回的IEnumerable&LT; T&GT; ,和一个返回一个的IQueryable&LT; T&GT; 。说,例如,你有一个产品表,你想获得的所有的成本是产品> $ 25的

For example, if you're working against a remote database, with many ORM systems, you have the option of fetching data from a table in two ways, one which returns IEnumerable<T>, and one which returns an IQueryable<T>. Say, for example, you have a Products table, and you want to get all of the products whose cost is >$25.

如果你这样做:

 IEnumerable<Product> products = myORM.GetProducts();
 var productsOver25 = products.Where(p => p.Cost >= 25.00);

在这里发生了什么,是数据库加载所有的产品,并将它们通过线路到您的程序。你的程序然后筛选数据。从本质上讲,数据库做了 SELECT * FROM产品,并返回每一个产品给你。

What happens here, is the database loads all of the products, and passes them across the wire to your program. Your program then filters the data. In essence, the database does a SELECT * FROM Products, and returns EVERY product to you.

有了正确的的IQueryable&LT; T&GT; 提供商,在另一方面,你可以这样做:

With the right IQueryable<T> provider, on the other hand, you can do:

 IQueryable<Product> products = myORM.GetQueryableProducts();
 var productsOver25 = products.Where(p => p.Cost >= 25.00);

在code看起来是一样的,但这里的不同之处在于执行的SQL将 SELECT * FROM产品在成本方式&gt; = 25

从您的POV作为一个开发者,这看起来是一样的。然而,从性能的角度来看,你可能只在网络上返回,而不是20000条记录....

From your POV as a developer, this looks the same. However, from a performance standpoint, you may only return 2 records across the network instead of 20,000....

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

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