简体   繁体   English

LINQ - 获取总查询结果时间

[英]LINQ - Get total query results time

I would like to know if it is possible to get a query 'search results' time? 我想知道是否有可能获得查询'搜索结果'的时间? Like how google has a time under each query - this is the time it took to complete the search . 就像谷歌每次查询都有时间一样 - 这是完成搜索所花费的时间

How is this achievable? 这怎么可以实现? I haven't attempted anything (hence no code) because, well, I'm not sure where to start. 我没有尝试任何东西(因此没有代码)因为,我不知道从哪里开始。

LINQPad has this feature built in, but I'd like to get something similar going on an internal mvc app I'm working on. LINQPad内置了这个功能,但是我想在我正在研究的内部mvc应用程序上获得类似内容。

Thanks 谢谢

You could use the Stopwatch class to measure the duration of the execution of your query (or any other code). 您可以使用Stopwatch类来测量查询(或任何其他代码)执行的持续时间。

Stopwatch sw = Stopwatch.StartNew();
var result = query.ToList();
TimeSpan elapsed = sw.Elapsed;
Console.WriteLine("Query took: " + elapsed);

In the case of LINQ queries, don't forget that these are only evaluated on demand using deferred execution. 对于LINQ查询,请不要忘记这些仅使用延迟执行按需评估。 Thus, you would probably want to force their enumeration (using, for example, ToList ) in order to get a true measure of their execution's duration. 因此,您可能希望强制进行枚举(例如,使用ToList )以获得其执行持续时间的真实度量。

您可以使用System.Diagnostics.Stopwatch对象,在页面开始渲染时启动它并在结束时停止它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM