获取 IQueryable的计数
假设我们有以下集合
IEnumerable<int> list = new List<int> { 11, 12, 13, 14, 15, 16, 17, 18, 19, 10 };
var query = list.Skip(5).Take(4);
SomeMethod(query.AsQueryable());
.
.
.
public void SomeMethod(IQueryable<T> query)
{
var collectionCount = ????? // count should be 10 not 4.
...
}
如何获取 SomeMethod 中查询的原始集合的计数(不应用 Skip 和 Take 子查询)。
谢谢。
Assume we have the following collection
IEnumerable<int> list = new List<int> { 11, 12, 13, 14, 15, 16, 17, 18, 19, 10 };
var query = list.Skip(5).Take(4);
SomeMethod(query.AsQueryable());
.
.
.
public void SomeMethod(IQueryable<T> query)
{
var collectionCount = ????? // count should be 10 not 4.
...
}
How can I get the count of the original collection (without applying Skip and Take sub-queries) of the query in the SomeMethod.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法获取这些信息(除非做一些硬核反射的事情)。它根本不存在了。就像您想从类似这样的内容中提取原始计数:
我认为您可能能够获取信息,如果您不传递
IQueryable
,而是传递Expression
然而,即使是这个案子也不会简单。每条评论添加:
这并不像你想象的那么简单;我什至不认为这完全可能。但是,对于某些有限的用例,它可能有效。我很快就破解了一种方法,该方法可以“删除”应用于查询的任何
Skip
aTake
并返回原始计数。尝试一下,YMMV:There is no way you could get the information (except maybe by doing some hardcore reflection stuff). It is simply not there anymore. It’s like you wanted to extract the original count from something like this:
I think you might be able to get the information if instead of
IQueryable
, you would passExpression
, however, even this case would not be simple.Added per comments:
It is not as simple as you might think; I don’t even think it is possible fully generally. However, for some limited use cases it might work. I have quickly hacked a method which “removes” any
Skip
aTake
applied to the query and returns the original count. Give it a try, YMMV:list.Count 应返回原始列表的计数
list.Count should return the count of the original list