none
Join 3 table RRS feed

  • Question

  • I want to join 3 table by LINQ , i did this :

     var results = northwind.Products.Join(northwind.Categories, c => c.CategoryID, d => d.CategoryID, (c, d) => new { c.ProductID, c.ProductName, c.SupplierID, d.CategoryName })
                                                .Where(q => q.ProductID > 12)
                                                .Join(northwind.Suppliers, f => f.SupplierID, g => g.SupplierID, (f, g)
    => new { f.CategoryName, f.ProductName, g.CompanyName, g.ContactName, g.Country });

    but the query is too long, who can tell me to write it shorter ?

    thank.

             
    Saturday, March 21, 2009 11:22 AM

All replies

  •  This is a different way to write it which is a little cleaner, but in the end both ways produce the same exact SQL on the back end.

    SELECT [t2].[CategoryName], [t0].[ProductName], [t1].[CompanyName], [t1].[ContactName], [t1].[Country]

    FROM [dbo].[Products] AS [t0]

    INNER JOIN [dbo].[Suppliers] AS [t1] ON [t0].[SupplierID] = ([t1].[SupplierID])

    INNER JOIN [dbo].[Categories] AS [t2] ON [t0].[CategoryID] = ([t2].[CategoryID])

    WHERE [t0].[ProductID] > 12



    var
    qresult = from p in db.Products

    where p.ProductID > 12

    join s in db.Suppliers on p.SupplierID equals s.SupplierID

    join c in db.Categories on p.CategoryID equals c.CategoryID

    select new {c.CategoryName, p.ProductName, s.CompanyName, s.ContactName, s.Country };


    joe alt
    • Proposed as answer by altja Saturday, March 21, 2009 2:28 PM
    Saturday, March 21, 2009 2:27 PM
  • Thank for help
    Tuesday, March 24, 2009 9:45 AM