为什么使用AsQueryable已()代替名单()? [英] Why use AsQueryable() instead of List()?

查看:308
本文介绍了为什么使用AsQueryable已()代替名单()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进入使用与实体框架数据访问Repository模式和 LINQ 作为实施非测试存储库的基础。我看大多数样品返回AsQueryable已()在调用返回N条记录而不是List< T>。什么是这样做的优势在哪里?

I'm getting into using the Repository Pattern for data access with the Entity Framework and LINQ as the underpinning of implementation of the non-Test Repository. Most samples I see return AsQueryable() when the call returns N records instead of List<T>. What is the advantage of doing this?

推荐答案

AsQueryable已只是创建一个查询,以获取列表所需的指令。您可以进一步的更改查询,如补充说,被发送一路下跌到数据库级新Where子句后。

AsQueryable just creates a query, the instructions needed to get a list. You can make futher changes to the query later such as adding new Where clauses that get sent all the way down to the database level.

AsList返回内存中的所有项目的实际列表。如果添加一个新的凡cluse它,你没有得到快速过滤数据库提供。相反,你得到列表中的所有信息,然后筛选出你没有在应用程序所需要的。

AsList returns an actual list with all the items in memory. If you add a new Where cluse to it, you don't get the fast filtering the database provides. Instead you get all the information in the list and then filter out what you don't need in the application.

所以基本上可以归结为表达之前等待,直到最后可能momment。

So basically it comes down to waiting until the last possible momment before committing yourself.

这篇关于为什么使用AsQueryable已()代替名单()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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