2012-12-02 31 views
2

我試圖在linq查詢之前使用秒錶,並在LINQ查詢之後停止它,但linq查詢需要2分鐘,但stopwtach結果給出40 MS的時間!如何測量LINQ Query的時間?

這是LINQ查詢

Stopwatch stopwatch = Stopwatch.StartNew(); 
    var sets = 
    from a in patient 
    from b in patient 
    from c in patient 
    from d in patient 
    from l in patient 
    where a.VisitNum < b.VisitNum && b.VisitNum < c.VisitNum && c.VisitNum < d.VisitNum && d.VisitNum < l.VisitNum 
    select new { a, b, c, d, l }; 
       stopwatch.Stop(); 

什麼建議嗎?

回答

3

LINQ使用延遲執行。
直到您枚舉結果爲止,查詢纔會執行。

要強制查詢執行,您可以撥打.Count()。通過轉換到一個列表

+0

難道很多的LINQ to SQL實現嵌入'.Count之間()'插入到SQL調用中,這意味着您的性能猜測會顯着受到影響,因爲您不必花費時間通過網絡發送對象。 – Guvante

+0

@Guvante:不適用於LINQ to objects。 – SLaks

2

,您可以強制stopwatch.Stop() 之前LINQ查詢的執行你的情況,這將是─

​​