8

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. With equals, 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 of equals, 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?

2
  • 3
    outersource join innersource on outerkey equals innerkey. Essentially, what they're saying is you cannot write the following: outersource join innersource on innerkey equals outerkey
    – Rob
    Nov 10, 2014 at 14:21
  • So now what is the semantic differences between the two in an IEnumerable context. I am sure this could help with IQueryable.
    – leppie
    Nov 11, 2014 at 16:10

1 Answer 1

11

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.