LINQ to SQL INNER AND LEFT JOINS
By AzamSharp
Views: 1136

Introduction:

In this video Mohammad Azam will demonstrate how to use LINQ to SQL inner and outer left joins. He will also compare the LINQ to SQL approach with the traditional T-SQL approach.

NorthwindDataContext db = new NorthwindDataContext();

            var query1 = (from c in db.Categories
                         join p in db.Products
                         on c.id equals p.CategoryID
                         select new { CategoryName = c.CategoryName }).Distinct();


            var query = (from c in db.Categories
                        join p in db.Products
                        on c.id equals p.CategoryID into joinedProducts

                        from joinedProduct in joinedProducts.DefaultIfEmpty()
                        select new { CategoryName = c.CategoryName }).Distinct();


            Console.WriteLine(query);

            foreach (var item in query)
            {
                Console.WriteLine(item.CategoryName);
            }

[Play the video]

 

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: LINQ
Name: RB
Date: 3/12/2008 10:54:00 AM
Comment:
It is very nice and with source code to run your demo. But what are the “using” statements you are using in this demo?

Thank you,
RB
Subject: RE: LINQ
Name: AzamSharp
Date: 3/12/2008 8:31:19 PM
Comment:
Hi RB, Not sure what you meant by the "using" statements. I think you are referring to the namespaces. I am using VS 2008 and C# 3.0 framework.
Subject: LINQ
Name: RB
Date: 3/13/2008 2:38:45 PM
Comment:
Thank you very much for your feedback.

Do you add other statements then:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

Thank you,
RB
Subject: LINQ
Name: RB
Date: 3/13/2008 2:38:52 PM
Comment:
Thank you very much for your feedback.

Do you add other statements then:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

Thank you,
RB



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008