EF 4.1 / Linq-to-SQL: which is better: using Equals or ==?

We discuss using Equals or == to compare ints in a LINQ query. First we use the EF4.1 Code First. What's better?

 var query = context.Boodschappen .Where(b => b.BoodschapID == id).FirstOrDefault(); 

or

 var query = context.Boodschappen .Where(b => b.BoodschapID.Equals(id)).FirstOrDefault(); 

And why ?

+3
source share
2 answers

For Linq To Sql, you do not want either one. Use Object.Equals: .Where(b => Object.Equals(b.BoodschapID, id)

Why? Due to an error in the SQL generator if the identifier is a unique unique identifier. If you use b.BoodschapID.Equals (id) or b.BoodschapID == id and b.BoodschapID, this is a NULL pointer created by SQL will not be WHERE BoodschapID IS NULL , but rather WHERE BoodschapID = @p0 , which will not return any results .

I know for sure that EF had the same error. Think about whether she has decided yet. You can find more details in this question , just keep in mind that some answers generate horrific SQL.

Also, there is no difference between Equals and == in Linq To SQL that I know of.

+9
source

I prefer the first one. It also works with properties with a null value.

0
source

All Articles