Tuesday, February 21, 2012

How to use Join and Orderby in Linq to Sql

Sometimes we get some difficulties when we want are operating with two tables and implementing LINQ to SQL then a simple question arises in our mind:
Can we apply join operation on two tables while using LINQ to SQL?
Here my answer is YES

LINQ to SQL is designed to support ordering by simple primitive types, such as string, int, datetime and so on. 

Example:
Here we have two tables Orders and Customers.
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
                          };

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Code Imagine