Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help Understanding Enumerable.Join Method

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.

like image 253
Christopher Garcia Avatar asked Mar 13 '10 19:03

Christopher Garcia


People also ask

How does Linq join work?

In LINQ, an inner join is used to serve a result which contains only those elements from the first data source that appears only one time in the second data source. And if an element of the first data source does not have matching elements, then it will not appear in the result data set.

How do you write Iqueryable join using lambda?

1); var join = query1. Join(query2, x => x. ParentId, y => y. ParentId, (query1, query2) => new { query1 , query2 }).

What is Groupjoin in Linq?

The group join is useful for producing hierarchical data structures. It pairs each element from the first collection with a set of correlated elements from the second collection. For example, a class or a relational database table named Student might contain two fields: Id and Name .

What is Linq enumerable?

Count Method (System. Linq) Returns the number of elements in a sequence.


2 Answers

Explanation of the Join.

b = object type of first table o = object type of first table i = object type of second table

  1. db.TableB.Where( b => b.MyField == someValue ) This is the element type of the second table
  2. o => o.someFieldID The key of the first table
  3. i => i.someFieldID The key of the second table (which will match the key in the first table)
  4. (o,i) => o The object to return, in this case the object type of the first table.
like image 55
Mikael Svenson Avatar answered Sep 21 '22 11:09

Mikael Svenson


This query is saying join TableA to TableB where TableA.someFieldID == TableB.someFieldID and selecting the results from TableA and seeing if there are any results at all

In terms of SQL think of it like this, even if it's not Linq-to-SQL...if you are familiar with SQL maybe this makes more sense:

Select Count(*)
From TableA a
     Join TableB b
       On a.someFieldID = b.someFieldID

Then checking if Count(*) is > 0

like image 27
Nick Craver Avatar answered Sep 17 '22 11:09

Nick Craver