>LINQ Operators – Part V

>

Joining operators:

Join: If you remember from query methods join is not needed in the case of direct relations in the model (however local queries are faster with join). You can create inner and left joins using Join operator. Syntax is simple:

Customers.Join(            // Customers
   Orders,                     // inner join Orders
   c => c.CustomerID,    // (Customers as c) on c.CustomerID
   o => o.CustomerID,   // (Orders as o) = o.OrderID
   (c, o) => new            // local aliases – select c.CustomerID, o.OrderID, o.OrderDate
       {
            c.CustomerID,
            o.OrderID,
            o.OrderDate
       } ) 
Is equivalent to (and generates) this SQL:
SELECT c.CustomerID, o.OrderID, o.OrderDate
FROM Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID

You can use a custom equality comparer delegate as a parameter.

GroupJoin: GroupJoin is  a type of join that groups the results into lists based on join key – list of lists where inner lists are "grouped" on key.  

Customers.GroupJoin(
   Orders,
   c => c.CustomerID,
   o => o.CustomerID,
   (input,outputSequence) => outputSequence
           .Select( r => new { r.CustomerID, r.OrderID, r.OrderDate } ) )
Advertisements

About cetinbasoz

A developer working primarily with C#,F#,VFP,SQL server, Silverlight,Linq ...
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s