Sunday, May 1, 2011

How to use joins and orderby in Linq to Sql


LINQ to SQL is designed to support ordering by simple primitive types, such as string, int,datetime and so on, for this example I have taken Orders and Customers tables in Northwind Database.
DataClasses1DataContext context = new DataClasses1DataContext();
            var Details = from cus in context.Customers
                          join ord in context.Orders on cus.CustomerID equals ord.CustomerID
                          orderby ord.OrderDate descending
                          select new
                          {
                              cus.Address,
                              cus.City,
                              cus.CompanyName,
                              cus.ContactName,
                              ord.OrderDate,
                              ord.ShipAddress,
                              ord.ShipVia
                          };

No comments:

Post a Comment