IQueryable的结构是怎样的?
using (var nosql = new DbHelper("Feed"))
{
var watch = Stopwatch.StartNew();
nosql.CollectionName = "rawhi";
var x = nosql.GetRecords<Event>(p => true, 0, 1000000);
//GridView1.DataSource = x;
//GridView1.DataBind();
watch.Stop();
long milliseconds = watch.ElapsedMilliseconds;
Response.Write(milliseconds);
}
x
是 IQueryable
类型的变量。
当我运行此代码时,结果是: 0
所以我想知道数据是否存储在 x
var 中?
using (var nosql = new DbHelper("Feed"))
{
var watch = Stopwatch.StartNew();
nosql.CollectionName = "rawhi";
var x = nosql.GetRecords<Event>(p => true, 0, 1000000);
//GridView1.DataSource = x;
//GridView1.DataBind();
watch.Stop();
long milliseconds = watch.ElapsedMilliseconds;
Response.Write(milliseconds);
}
x
is a variable of type IQueryable
.
When I run this code the result is: 0
So I'm wondering if the data is stored in the x
var or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询将被延迟计算,因此在尝试枚举结果之前,查询不会实际执行或返回结果。在您的代码示例中,您已经设置了查询,但尚未实际运行它。如果您将数据绑定代码放回原处,那么实际上会枚举结果并执行它。
出于测试目的,您可以按如下方式强制枚举:
The query will be lazily evaluated, so until something tries to enumerate the results, the query is not actually executed or the results returned. In your code example you have set the query up, but you have not actually run it. If you put your databinding code back in, that will actually enumerate the result and so execute it.
For testing purposes you can force enumeration as follows:
可以这么说,您只设置了管道 - 您现在将能够通过 x 检索结果,但只有在您执行此操作时才会延迟检索它们。
您可以一一检索它们:
有时急切地检索数据是有利的,在这种情况下,您可以使用
ToList()
它在内部枚举所有结果并将结果集放入 x - 然后您可以测量需要多长时间:You only set up the piping so to speak - you now will be able to retrieve the results through
x
but they are lazily retrieved only when you do it.You could retrieve them one by one:
Sometimes its advantageous to eagerly retrieve data, in that case you can use
ToList()
which enumerates all the results internally and puts the result set in x - then you can measure how long it would take: