Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq: What is the difference between == and equals in a join?

Tags:

c#

join

linq

equals

I always wondered why there's an equals keyword in linq joins rather than using the == operator.

Property deadline = (from p in properties  join w in widgets     on p.WidgetID equals w.ID  select p).First(); 

Instead of

Property deadline = (from p in properties  join w in widgets     on p.WidgetID == w.ID  select p).First(); 

[EDIT] Rephrased the question and revised the examples.

like image 650
Michael Klement Avatar asked Jul 14 '09 07:07

Michael Klement


People also ask

What is the difference between == and Equals () in C#?

Difference between == and . Equals method in c# The Equality Operator ( ==) is the comparison operator and the Equals() method in C# is used to compare the content of a string. The Equals() method compares only content.

Which is better Equals or == in C #?

Answering to the point “There is no difference between equality comparison using “==” and “Equals()”, except when you are comparing “String” comparison. The common comparison Rule :-Whenever youare comparing variables they are either value types or reference types.

How does join work in LINQ?

In a LINQ query expression, join operations are performed on object collections. Object collections cannot be "joined" in exactly the same way as two relational tables. In LINQ, explicit join clauses are only required when two source sequences are not tied by any relationship.

What is the difference between A Equals B and a == b in C#?

== operator compares reference returns true when both references point to the same object and Equals() compares object by value and it will return true if the references refers object which are equivalent. How to get the URL of the current page in C# - Asp.Net?


2 Answers

There's a nice explanation by Matt Warren at The Moth:

"The reason C# has the word ‘equals’ instead of the ‘==’ operator was to make it clear that the ‘on’ clause needs you to supply two separate expressions that are compared for equality not a single predicate expression. The from-join pattern maps to the Enumerable.Join() standard query operator that specifies two separate delegates that are used to compute values that can then be compared. It needs them as separate delegates in order to build a lookup table with one and probe into the lookup table with the other. A full query processor like SQL is free to examine a single predicate expression and choose how it is going to process it. Yet, to make LINQ operate similar to SQL would require that the join condition be always specified as an expression tree, a significant overhead for the simple in-memory object case."

However, this concerns join. I'm not sure equals should be used in your code example (does it even compile?).

like image 127
Konrad Rudolph Avatar answered Sep 19 '22 14:09

Konrad Rudolph


Your first version doesn't compile. You only use equals in joins, to make the separate halves of the equijoin clear to the compiler.

like image 22
Jon Skeet Avatar answered Sep 19 '22 14:09

Jon Skeet