none
How to write Join query as Lambda Expression? RRS feed

  • Question

  • Hi, I am having difficulties to understand the labda expression with 3 parameters.
    Basically I need to use LINQ join as a lambda expression.

    What I am trying to achieve is to write the same query that starts on line 23 with lambda expression syntax on line 30.


    1     public class Client  
    2     {  
    3         public string Id { get; set; }  
    4     }  
    5     public class Order  
    6     {  
    7         public string Id { get; set; }  
    8         public string ClientId { get; set; }  
    9     }  
    10     public class DataBase  
    11     {  
    12         public List<Client> Clients { get; set; }  
    13         public List<Order> Orders { get; set; }  
    14     }  
    15  
    16     public class Program  
    17     {  
    18         static void Main(string[] args)  
    19         {  
    20             // get database  
    21             var db = GetDataBase();  
    22  
    23             var query = from clients in db.Clients  
    24                     join orders in db.Orders on clients.Id equals orders.ClientId  
    25                     select new { Clients = clientsOrders = orders };  
    26  
    27             //  
    28             // HOW TO WRITE IT AS A LAMBDA EXPRESSION ??????  
    29             //  
    30             //var query = db.Clients.Join(db.Orders, client => client.Id, order => order.ClientId, client  
    31  
    32             foreach (var item in query)  
    33             {  
    34                 Console.WriteLine(string.Format("ClientId = {0}, OrderId = {1}", item.Clients.Id, item.Orders.Id));  
    35             }  
    36  
    37         }  
    38  
    39         private static DataBase GetDataBase()  
    40         {  
    41             // create clients  
    42             var client1 = new Client() { Id = "Client 1" };  
    43             var client2 = new Client() { Id = "Client 2" };  
    44  
    45             // create orders and add them to client 1  
    46             var order1 = new Order() { Id = "Order 1"ClientId = client1.Id };  
    47             var order2 = new Order() { Id = "Order 2"ClientId = client1.Id };  
    48  
    49             // create order without client id  
    50             var order3 = new Order() { Id = "Order 3" };  
    51  
    52             // create database  
    53             var db = new DataBase() { Clients = new List<Client>(), Orders = new List<Order>() };  
    54             db.Clients.Add(client1);  
    55             db.Clients.Add(client2);  
    56  
    57             db.Orders.Add(order1);  
    58             db.Orders.Add(order2);  
    59             db.Orders.Add(order3);  
    60  
    61             return db;  
    62         }  
    63     }  
    64  
     

    Thanks,
    Edijs
    Monday, July 28, 2008 8:01 AM

Answers

  • This

    var query = from clients in db.Clients  
             join orders in db.Orders on clients.Id equals orders.ClientId  
             select new { Clients = clients, Orders = orders }; 
    
    

    becomes

    var query = db.Clients.Join( orders,       // Target table
                  ( cl => cl.Id ),   // ID of clients column
                  ( or => or.ClientID), // Corresponding ID for orders to join on
                  ( ( cl, or ) => new { Clients = cl, Orders = or } ));
    
                         
    

    William Wegerson (www.OmegaCoder.Com)
    Thursday, January 20, 2011 9:00 PM
    Moderator

All replies

  • For those kinds of questions LinqPad is an excelent tool.

    www.linqpad.net
    • Marked as answer by jack 321 Thursday, July 31, 2008 3:38 AM
    • Unmarked as answer by OmegaManModerator Friday, July 16, 2010 8:50 PM
    Monday, July 28, 2008 9:20 AM
  • Thanks Thomas, LINQPad is a very good tool!
    Monday, July 28, 2008 1:52 PM
  • This

    var query = from clients in db.Clients  
             join orders in db.Orders on clients.Id equals orders.ClientId  
             select new { Clients = clients, Orders = orders }; 
    
    

    becomes

    var query = db.Clients.Join( orders,       // Target table
                  ( cl => cl.Id ),   // ID of clients column
                  ( or => or.ClientID), // Corresponding ID for orders to join on
                  ( ( cl, or ) => new { Clients = cl, Orders = or } ));
    
                         
    

    William Wegerson (www.OmegaCoder.Com)
    Thursday, January 20, 2011 9:00 PM
    Moderator