C# 为什么使用 AsQueryable() 而不是 List()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1106802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:23:40  来源:igfitidea点击:

Why use AsQueryable() instead of List()?

c#linqrepositoryiqueryable

提问by Keith Adler

I'm getting into using the Repository Pattern for data access with the Entity Frameworkand LINQas 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?

我开始使用 Repository Pattern 进行数据访问,Entity FrameworkLINQ作为非测试存储库实现的基础。当调用返回 N 条记录而不是 List<T> 时,我看到的大多数示例都返回 AsQueryable()。这样做有什么好处?

采纳答案by Jonathan Allen

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.

AsQueryable 只是创建一个查询,获取列表所需的指令。您可以稍后对查询进行进一步更改,例如添加一直发送到数据库级别的新 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.

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

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

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

回答by dahlbyk

Returning IQueryable<T>will defer execution of the query until its results are actually used. Until then, you can also perform additional database query operations on the IQueryable<T>; on a Listyou're restricted to generally less-efficient in-memory operations.

返回IQueryable<T>将推迟查询的执行,直到其结果被实际使用。在此之前,您还可以对IQueryable<T>;执行额外的数据库查询操作。在 a 上,List您仅限于通常效率较低的内存操作。

回答by Daniel Brückner

Returning IQueryable<T>has the advantage, that the execution is defferer until you really start enumerating the result and you can compose the query with other queries and still get server side execution.

返回IQueryable<T>的优点是,在您真正开始枚举结果之前,执行会延迟,并且您可以将查询与其他查询组合在一起,并且仍然获得服务器端执行。

The problem is that you cannot control the lifetime of the database context in this method - you need an open context and must ensure that it stays open until the query gets executed. And then you must ensure that the context will be disposed. If you return the result as a List<T>, T[], or something similar, you loose deffered execution and server side execution of composed queries, but you win control over the lifetime of the database context.

问题是您无法在此方法中控制数据库上下文的生命周期 - 您需要一个打开的上下文,并且必须确保它在执行查询之前保持打开状态。然后您必须确保上下文将被处理。如果返回的结果为List<T>T[]或者类似的东西,你失去递延执行和组成查询的服务器端执行,但你赢得了数据库上下文的寿命控制。

What fits best, of course, depends on the actual requirements. It's another question without a single truth.

当然,什么最适合取决于实际要求。这是另一个没有单一真理的问题。

回答by Olmo

AsQueryableis an extension method for IEnumerable<T>that could do two things:

AsQueryable是一个扩展方法IEnumerable<T>,可以做两件事:

  • If the IEnumerable<T>implements IQueryable<T>justs casts, doing nothing.
  • Otherwise creates a 'fake' IEnumerable<T>(EnumerableQuery<T>) that implements every method compiling the lambdas and calling to Enumerable extension methods.
  • 如果IEnumerable<T>工具IQueryable<T>只是投射,什么都不做。
  • 否则会创建一个“假” IEnumerable<T>( EnumerableQuery<T>),它实现了编译 lambdas 和调用 Enumerable 扩展方法的每个方法。

So in most of the cases using AsQueryable is useless, unless u are forced to pass a IQueryable to a method and u have a IEnumerable instead, it's a hack.

所以在大多数情况下,使用 AsQueryable 是没用的,除非你被迫将 IQueryable 传递给一个方法,而你有一个 IEnumerable,否则这是一个黑客。

NOTE: AsQueryable is a hack, IQueryable of course is not!

注意:AsQueryable 是一个 hack,IQueryable 当然不是!