+92 332 4229 857 99ProjectIdeas@Gmail.com

Applying LINQ On Class Part 2 (C#.net)




Applying LINQ On Class Part 2 (C#.net):
The following code shows you how to define a class, creating lists of objects. and then applying queries on the data. See the code below.


Code



public class Orders
    {
        private int _OrderID;
        private string _ItemName;
        private int _CustomerID;
        private double _Cost;
       
        public int OrderID
        {
            get { return _OrderID; }
            set { _OrderID = value; }
        }

        public string ItemName
        {
            get { return _ItemName; }
            set { _ItemName = value; }
        }

        public int CustomerID
        {
            get { return _CustomerID; }
            set { _CustomerID = value; }
        }
       
        public double Cost
        {
            get { return _Cost; }
            set { _Cost = value; }
        }
    }

public class Customer
    {
        private int _CustomerID;
        private string _Name;
        private string _Email;
       
        public int CustomerID
        {
            get { return _CustomerID; }
            set { _CustomerID = value; }
        }
       
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
      
        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }
    }

// 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 1 : Getting data from Orders table(orderList) where customer Id = 15
var foundOrders =
                from Orders o in orderList
                where (o.CustomerID == 15)
                select o;
            foreach (Orders ord in foundOrders)
            {
                Console.WriteLine("Order Id : " + ord.OrderID);
                Console.WriteLine("Customer Id : " + ord.CustomerID);
                Console.WriteLine("Item Name : " + ord.ItemName);
                Console.WriteLine("Item Cost : " + ord.Cost);
            }
Output:

Order Id : 2
Customer Id : 15
Item Name : Chips
Item Cost : 18.5



// Query 2 : Getting data from orderList where customerID = 20 and applying tax 1.5% on cost
var foundOrders =
                from Orders o in orderList
                where (o.CustomerID == 20)
                select new { //Anonymous Type
                    o.OrderID,
                    o.ItemName,
                    o.CustomerID,
                    o.Cost,
                   CostWithTax = o.Cost * 1.5
                };

foreach (var ord in foundOrders)
            {
                Console.WriteLine("Order Id : " + ord.OrderID);
                Console.WriteLine("Customer Id : " + ord.CustomerID);
                Console.WriteLine("Item Name : " + ord.ItemName);
                Console.WriteLine("Item Cost : " + ord.Cost);
                Console.WriteLine("Item Cost With Tax : " + ord.CostWithTax);
            }

Output:

Order Id : 3
Customer Id : 20
Item Name : Deodorant
Item Cost : 99.9
Item Cost With Tax : 149.85

Order Id : 4
Customer Id : 20
Item Name : Shaving Gel
Item Cost : 170
Item Cost With Tax : 255


// Query 3 : Getting itemName and it’s cost from orderList in descending order
var foundOrders =
                from Orders o in orderList
                orderby o.Cost descending  //can be ascending as well
                select new {
                     o.ItemName,
                     o.Cost
                };
foreach(var ord in foundOrders)
           Console.WriteLine("Item Name : " + ord.ItemName + " , Cost : " + ord.Cost);
               
            
Output:

Item Name : Shaving Gel , Cost : 170
Item Name : Deodorant , Cost : 99.9
Item Name : Lipstick , Cost : 70
Item Name : Biscuit , Cost : 49.5
Item Name : Chips, Cost : 18.5


Applying LINQ On Class Part 1 (C#.net)

0 comments: