码迷,mamicode.com
首页 > 编程语言 > 详细

IEnumerable<T>和IQuryable<T>的区别

时间:2019-05-13 14:39:16      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:wan   hand   main   The   man   fetch   support   cos   sse   

 

https://stackoverflow.com/questions/1578778/using-iqueryable-with-linq/1578809#1578809

 


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.

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.

If you do:

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

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.

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);

The code looks the same, but the difference here is that the SQL executed will be SELECT * FROM Products WHERE Cost >= 25.

IEnumerable<T>和IQuryable<T>的区别

标签:wan   hand   main   The   man   fetch   support   cos   sse   

原文地址:https://www.cnblogs.com/dayang12525/p/10856172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!