I have been reading Programming Microsoft LINQ in Microsoft .NET Framework 4, and now I am understanding the join
clause in LINQ, but I have a doubt or question respect about its definition; in the book it is defines as:
You can define equality comparisons only by using a special
equals
keyword that behaves differently from the==
operator, because the position of the operands is significant. Withequals
, the left key consumes the outer source sequence, and the right key consumes the inner source sequence. The outer source sequence is in scope only on the left side ofequals
, and the inner source sequence is in scope only on the right side.
And there is also a formal definition about this operator:
join-clause ::= join innerItem in innerSequence on outerKey equals innerKey
Please, can someone explain me the above concept in other words or by paraphrasing it?
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.
equals() method. The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.
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.
The Equals() and == operator are used for comparison and both returns the boolean value (true/false). For Value Type == and Equals() works in the same way, both compare two object by value. a==b and a. Equals(b) returns true , because in this case both compare two object by value.
I guess it's because 'equals' in the join doesn't work like == does, and so the language designers decided not to call what it is doing the same thing.
In C#, it is sort of given that a == b is exactly the same as b == a. In the definition of a join, this is not so:
var list = from a in ctx.TableA
join b from ctx.TableB on a.Id equals b.tableAId
This, above, is valid.
var list = from a in ctx.TableA
join b from ctx.TableB on b.tableAId equals a.Id
This will not compile. What the language spec says is that the 'outer' table (TableA in this case) must be specified first and the inner one (TableB) must be second. I suppose that the language designers thought that this was sufficiently different from the way that == works that it would be a bad idea to use it and they came up with the idea to use 'equals'.
I think I'm probably right, but only the language designers involved will really know the truth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With