This is a mirror of official site: http://jasper-net.blogspot.com/

LINQ – Left Join Example in C#

| Monday, January 31, 2011
In this post, we will see an example of how to do a Left Outer Join in LINQ and C#.

In a previous post, we saw how to do an Inner join in C# and LINQ where each element of the first collection appears one time for every matching element in the second collection. If an element in the first collection has no matching elements, it does not appear in the join result set. However in a Left Outer Join, each element of the first collection is returned, regardless of whether it has any correlated elements in the second collection.

Let us see this with an example.

class Program
{
   static void Main(string[] args)
   {
       List<Book> bookList = new List<Book>
       {
           new Book{BookID=1, BookNm="DevCurry.com Developer Tips"},
           new Book{BookID=2, BookNm=".NET and COM for Newbies"},
           new Book{BookID=3, BookNm="51 jQuery ASP.NET Recipes"},
           new Book{BookID=4, BookNm="Motivational Gurus"},
           new Book{BookID=5, BookNm="Spiritual Gurus"}
       };

       List<Order> bookOrders = new List<Order>{
           new Order{OrderID=1, BookID=1, PaymentMode="Cheque"},
           new Order{OrderID=2, BookID=5, PaymentMode="Credit"},
           new Order{OrderID=3, BookID=1, PaymentMode="Cash"},
           new Order{OrderID=4, BookID=3, PaymentMode="Cheque"},
           new Order{OrderID=5, BookID=5, PaymentMode="Cheque"},
           new Order{OrderID=6, BookID=4, PaymentMode="Cash"}
       };
   }
}

public class Book
{
   public int BookID { get; set; }
   public string BookNm { get; set; }
}

public class Order
{
   public int OrderID { get; set; }
   public int BookID { get; set; }
   public string PaymentMode { get; set; }
}
}
Let us do a Left Outer Join between the Book and Order collection

var orderForBooks = from bk in bookList
           join ordr in bookOrders
           on bk.BookID equals ordr.BookID
           into a
           from b in a.DefaultIfEmpty(new Order())
           select new
           {
               bk.BookID,
               Name = bk.BookNm,
               b.PaymentMode
           };

foreach (var item in orderForBooks)
   Console.WriteLine(item);

Console.ReadLine();

Read more: Devcurry

Posted via email from Jasper-net

0 comments: