IQueryable到底是什么意思? [英] What exactly does IQueryable mean?

查看:494
本文介绍了IQueryable到底是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我确切地理解IQueryable是什么.

I'm not sure I understand what exactly IQueryable is.

我了解它并不能为我提供所有实体,而是将约束部分放入内存中,但会将其作为发送到数据库的命令的一部分执行.

I understand that it doesn't get me all the entities and puts the constraint part in memory but executes it as part of the command it sends to the database.

如果我弄错了,请纠正我,并告诉我,为什么我的存储库函数返回IQueryable而不是简单的List?

Please correct me if I'm mistaken and tell me, why should my repository functions return IQueryable and not a simple List?

推荐答案

IQueryable实际上不是实体的集合,而是描述了如何获取该集合.除非对数据进行评估(将其转换为IEnumerable或类似名称),否则不会从源中检索数据.

An IQueryable is not actually a collection of entities, rather it describes how to obtain that collection. The data is never retrieved from the source until you evaluate it, by turning it into an IEnumerable or something similar.

从存储库返回IQueryable的优点是,调用代码可以在查询进入数据库之前指定其他.Where子句.如果返回IEnumerable,则代码将从数据库中检索一个表的所有值,然后在内存中执行过滤,这效率很低.

The advantage of returning an IQueryable from your repository is that your calling code can specify additional .Where clauses BEFORE the query goes to the database. If you returned an IEnumerable, your code would retrieve all values of one table from the database, and then perform filtering in memory, which is inefficient.

这篇关于IQueryable到底是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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