Yesterday I posted this question regarding using lambdas inside of a Join() method to check if 2 conditions exist across 2 entities. I received an answer on the question, which worked perfectly. I thought after reading the MSDN article on the Enumerable.Join() method, I’d understand exactly what was happening, but I don’t. Could someone help me understand what’s going on in the below code (the Join() method specifically)? Thanks in advance.
if (db.TableA.Where( a => a.UserID == currentUser )
.Join( db.TableB.Where( b => b.MyField == someValue ),
o => o.someFieldID,
i => i.someFieldID,
(o,i) => o )
.Any())
{
//...
}
Edit:
Specifically, I’m curious about the last 3 parameters, and what’s actually going on. How do they result in the signature requirements of Func(TOuter, TKey), Func(TInner, TKey) etc.
Eric and Nick have both provided good answers.
You may also write the Linq query expression using query syntax (vs. method syntax, which you are using in your example):
Update:
You seem to be stuck on the lambda expressions. It’s a function that you pass around like a variable. A lambda expression is equivalent to an anonymous delegate (or anonymous method, to me more general).
Here is your query with the lambda expressions as delegates (replace EntityType with the type of your entity returned from TableA, of course):
{
//…
}
NOTE: A lambda expression has important aspects that make it more than just an equivalent for anonymous methods. I recommend that you look through other SO questions and read online about lambda expressions in particular. They allow for very powerful ideas to be expressed in a much simpler and elegant way. It’s a deep topic, but the basics are easy enough to understand. It’s a function that you can pass around like a variable, or as a parameter to other functions.