Wriju's BLOG

.NET, Cloud and everything

LINQ: Custom Object Query


Applies To: .NET Framework 3.0, LINQ


 


Hello all, I have already discussed about the LINQ model architecture and the query process for .NET object in my previous posts. But how about the object written by you? Most of the time we write our own object and return the collection. LINQ is applicable for any collection that implements the IEnumerable<T> e.g., Generics. Here as an example


 


I have defined a very simple customer class 


class Customer


    {


        private int _CustID;


        public int CustID


        {


            get { return _CustID; }


            set { _CustID = value; }


        }


 


        private string _CustName;


        public string CustName


        {


            get { return _CustName; }


            set { _CustName = value; }


        }


 


        private int _CustAge;


        public int CustAge


        {


            get { return _CustAge; }


            set { _CustAge = value; }


        }


 


        public Customer(int intCustID, string strCustName, int intCustAge)


        {


            _CustID = intCustID;


            _CustName = strCustName;


            _CustAge = intCustAge;


        }  


    }


 


Then I will create a Generic collection out of this class and execute the LINQ. Lets see how we can do that.


 


using System;


using System.Collections.Generic;


using System.Text;


using System.Query;


using System.Xml.XLinq;


using System.Data.DLinq;


 


namespace LINQConsoleApplication_Object


{


    class Program


    {


        static void Main(string[] args)


        {


            List<Customer> customers = GetCustomers();


           


            var q = from c in customers


                    where c.CustAge > 20


                    select c;


           


            Console.WriteLine(“Results”);


            Console.WriteLine(“==========================”);


            foreach(var val in q)


            {


                Console.WriteLine(“[{0}] {1} : {2}”, val.CustID, val.CustName, val.CustAge);


            }


            Console.ReadKey();


        }


 


        /// <summary>


        /// Create the Generic collection


        /// </summary>


        /// <returns></returns>


        static List<Customer> GetCustomers()


        {


            List<Customer> custs = new List<Customer>();


 


            custs.Add(new Customer(1, “A”, 10));


            custs.Add(new Customer(2, “B”, 20));


            custs.Add(new Customer(3, “C”, 30));


            custs.Add(new Customer(4, “D”, 40));


            custs.Add(new Customer(5, “E”, 50));


            custs.Add(new Customer(6, “F”, 10));


            custs.Add(new Customer(7, “G”, 20));


            custs.Add(new Customer(8, “H”, 30));


            custs.Add(new Customer(9, “I”, 40));


            custs.Add(new Customer(10, “J”, 50));


            custs.Add(new Customer(11, “K”, 60));


            custs.Add(new Customer(12, “L”, 20));


            custs.Add(new Customer(13, “M”, 30));


            custs.Add(new Customer(14, “N”, 40));


            custs.Add(new Customer(15, “O”, 10));


 


            return custs;


        }


    }


}


 


The out put will look like


 







Results


==========================


[3] C : 30


[4] D : 40


[5] E : 50


[8] H : 30


[9] I : 40


[10] J : 50


[11] K : 60


[13] M : 30


[14] N : 40