Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm going to try to convince you that:</p> <ul> <li>The two methods you proposed give the same performance.</li> <li>There are at least two non-performance related reasons you should prefer <code>==</code>.</li> <li>There is another separate improvement that you can make to your code to reduce the possibility of errors.</li> </ul> <p>To see that the performance will be the same, look at the SQL generated in each case. This test program shows you how you can view the generated SQL:</p> <pre><code>int orderId = 4; TextWriter textWriter = new StringWriter(); using (var dc = new DataClasses1DataContext()) { dc.Log = textWriter; Order o1 = dc.Orders.FirstOrDefault(x =&gt; x.OrderId == orderId); Order o2 = dc.Orders.FirstOrDefault(x =&gt; x.OrderId.Equals(orderId)); } string log = textWriter.ToString(); </code></pre> <p>The SQL sent in each case is the same, as you can see by inspecting the log:</p> <pre><code>SELECT TOP (1) [t0].[OrderId], [t0].[CustomerID], [t0].[Date], [t0].[Description] FROM [dbo].[Order] AS [t0] WHERE [t0].[OrderId] = @p0 SELECT TOP (1) [t0].[OrderId], [t0].[CustomerID], [t0].[Date], [t0].[Description] FROM [dbo].[Order] AS [t0] WHERE [t0].[OrderId] = @p0 </code></pre> <p>Regarding whether to use <code>==</code> or <code>Equals</code>, firstly I'd suggest using <code>==</code> for readability. This is the idiomatic way to compare two integers in C#.</p> <p>Secondly with <code>==</code> you will get a compile time error if you give objects of different (incompatible) types. I assume that in your case <code>order</code> has type int, but let's assume that someone else wrote this code and accidentally made an error where <code>order</code> is a variable of type <code>Order</code> instead of an <code>int</code>. Now let's compare what would happen in each case:</p> <pre><code>Order order = new Order { OrderId = 4 }; x.OrderId.Equals(order) // This compiles, but you get an exception at runtime: // Could not format node 'Value' for execution as SQL. x.OrderId == order // Compile error: Operator '==' cannot be applied to // operands of type 'int' and 'Order' </code></pre> <p>It is better to get compile time errors than runtime errors, so prefer to use <code>==</code> in this case.</p> <p>Finally, if you only expect one result you should prefer to use <code>SingleOrDefault</code> instead of <code>FirstOrDefault</code> as the former will throw an exception if there are two matching objects found instead of just returning the first. This extra check will cost a tiny amount in performance but again allows you to catch errors earlier. If performance is a critical issue for you, instead of removing these safety checks you should consider fetching multiple objects from the database at once, not one object at a time.</p> <p>So in summary I recommend that you use this:</p> <pre><code>Album album = db.Albums.SingleOrDefault(x =&gt; x.OrderId == orderId); </code></pre>
 

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