Friday, 2 September 2016

Select custom collections using LINQ in Console Application.


Here we have an example to select custom data using LINQ. 

Previous Blog:

Create XML result from database using LINQ.


We have a customer information and we will select it using LINQ.

class Program
    {
        static void Main(string[] args)
        {
            CustomData(); 
        }

        public static void CustomData()
        {
            var results = from m in LoadCustomData()
                                    select m;
            foreach (CustomerInfo item in results)
            {
         Console.WriteLine("Name:{0},Address:{1},Mobile:{2}",item.Name,item.Address,item.Mobile);            
            }
            Console.ReadKey();
        }

        public static List<CustomerInfo> LoadCustomData()
        {
            List<CustomerInfo> customer = new List<CustomerInfo>();
            customer.Add(new CustomerInfo { Name = "Ashraf", Address = "Delhi", Mobile = "8789789" });
            customer.Add(new CustomerInfo { Name = "Sheikh", Address = "Noida", Mobile = "567778" });
            customer.Add(new CustomerInfo { Name = "Sachin", Address = "Bangalore", Mobile = "57676787" });
            customer.Add(new CustomerInfo { Name = "Sunil", Address = "Mysore", Mobile = "788789908" });

            return customer;
        }

}

Created a class for Customer Information:

 public class CustomerInfo
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Mobile { get; set; }
    }

Snaps:



Output :


No comments:

Post a Comment

Join US Our Community
×