Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are mixing things here. You are talking about collections, but you don't seem to use LINQ to objects but you are querying a database. </p> <p><strong>LINQ to objects:</strong><br> <a href="http://msdn.microsoft.com/en-us/library/bb534972.aspx" rel="noreferrer"><code>Enumerable.Any</code></a> and <a href="http://msdn.microsoft.com/en-us/library/bb549039.aspx" rel="noreferrer"><code>Enumerable.FirstOrDefault</code></a> should perform the same, because their code is near identical:</p> <p><code>FirstOrDefault</code>: </p> <pre><code>foreach (TSource source1 in source) { if (predicate(source1)) return source1; } return default (TSource); </code></pre> <p><code>Any</code>:</p> <pre><code>foreach (TSource source1 in source) { if (predicate(source1)) return true } return false; </code></pre> <p><strong>LINQ to some database:</strong><br> You are using Entity Framework, LINQ to SQL or NHibernate and use <a href="http://msdn.microsoft.com/en-us/library/bb534338.aspx" rel="noreferrer"><code>Queryable.Any</code></a> and <a href="http://msdn.microsoft.com/en-us/library/bb549141.aspx" rel="noreferrer"><code>Queryable.FirstOrDefault</code></a> on the corresponding data context.<br> In this case, there are really no collections, because these calls are not executed on in memory objects but translated to SQL. </p> <p>This means, the performance difference stems from how the LINQ provider translates the code to SQL, so the best would be to first check the statements created. Are they equivalent? Or are they very different (<code>select count(0) from X</code> vs. <code>select top 1 from X</code>)? Than the difference might lie in the query optimizer of the DB, indexes and what not...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload