为什么使用 AsQueryable() 而不是 List()? [英] Why use AsQueryable() instead of List()?

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

问题描述

我开始使用存储库模式通过 实体框架进行数据访问和 LINQ 作为非测试存储库实现的基础.当调用返回 N 条记录而不是 List 时,我看到的大多数示例都返回 AsQueryable().这样做有什么好处?

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 返回一个包含内存中所有项目的实际列表.如果向其添加新的 Where 线索,则无法获得数据库提供的快速过滤.相反,您获取列表中的所有信息,然后过滤掉应用程序中不需要的信息.

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.

所以基本上归结为等到最后可能的时刻再承诺.

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

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

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