+92 332 4229 857 99ProjectIdeas@Gmail.com

LINQ Joining two classes data




LINQ Joining two classes data:
The following code shows how to join two classes together and apply query on them to get your required data. Just like database where we join two tables data. See the code below.


Code


// Creating orderList and adding Orders objects in the list

List<Orders> orderList = new List<Orders> {
            new Orders
            {
                OrderID = 1,
                ItemName = "Biscuit",
                CustomerID = 10,
                Cost = 49.9
            },
            new Orders
            {
                OrderID = 2,
                ItemName = "Chips",
                CustomerID = 15,
                Cost = 18.50
            },
            new Orders
            {
                OrderID = 3,
                ItemName = "Deodarant",
                CustomerID = 20,
                Cost = 99.9
            },
            new Orders
            {
                OrderID = 4,
                ItemName = "Shaving Gel",
                CustomerID = 20,
                Cost = 170
            },
            new Orders
            {
                OrderID = 5,
                ItemName = "Lipstick",
                CustomerID = 25,
                Cost = 70
            }


// Creating customerList and adding Customer objects in the list

List<Customer> customerList = new List<Customer>
            {
               new Customer {
                        CustomerID = 10,
                        Name = "Osama",
                        Email = "osamammursleen@gmail.com"
                    },
               new Customer {
                        CustomerID = 15,
                        Name = "Saad",
                        Email = "saadbinsaulat@gmail.com"
                    },
               new Customer {
                        CustomerID = 20,
                        Name = "Bilal",
                        Email = "bilalkhan_leo@hotmail.com"
                    },
               new Customer {
                        CustomerID = 25,
                        Name = "Maryam",
                        Email = "maryamali@hotmail.com"
                    },
               new Customer {
                        CustomerID = 30,
                        Name = "Zainab Sheikh",
                        Email = "zainabsheikh@yahoo.com"
                    }
            };


// Query: Getting Data From orderList & customerList Using Joining On CutomerID's
var customerOrders =
         from Orders o in orderList
         join c in customerList on o.CustomerID equals c.CustomerID
         select new { // Anonymous type
                c.Name,
                o.OrderID,
                o.ItemName,
                o.Cost
         };

foreach (var ord in customerOrders)
   {
       Console.WriteLine(ord.Name + " spent : " + ord.Cost + " Rs , on orderID : " + ord.OrderID + " , Item Name : " + ord.ItemName);
   }
        
Output:
       
Osama spent : 49.9 Rs , on orderID : 1 : Item Name : Biscuit
Saad spent  : 18.59 Rs , on orderID : 2 : Item Name : Chips
Bilal spent : 99.9 Rs , on orderID : 3 : Item Name : Deodorant        
Bilal spent : 170 Rs , on orderID : 4  : Item Name : Shaving Gel
Maryam spent : 70 Rs , on orderID : 5  : Item Name : Lipstick


0 comments: