Tuesday, January 27, 2015

LINQ examples



LINQ examples
C# Lambda Expressions
Code:
string[] names = { "Kisan", "Devang", "Ravi", "Ujas", "Karan" };

            IEnumerable query = names
                      .Where(n => n.Contains("n"))
                      .Select(n => n.ToUpper());

            foreach (var name in query)
                Console.WriteLine(name);
            Console.WriteLine("Press any key to continue ...");
            Console.ReadLine();
Result
LINQ Filtering Operator
Code:
string[] names = { "Kisan", "Ravi", "Ujas", "Karan", "Ketul" };
           
            Console.WriteLine("Name start with K:");
            IEnumerable query1 = names.Where(name => name.StartsWith("K"));
            foreach (string name in query1)
            {
                Console.WriteLine(name);
            }
            Console.WriteLine("Name end with n: ");
            IEnumerable query2 = from n in names where n.EndsWith("n") select n;
            foreach (string name in query2)
            {
                Console.WriteLine(name);
            }
            Console.WriteLine("Press any key to continue ...");
            Console.ReadLine();
Result
LINQ “let” Operator
Code:
class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons = new List<Person>{
                                            new Person{ Name = "Kisan", City="Ladol" },
                                            new Person{ Name = "Ravi", City = "Ladol" },
                                            new Person{ Name = "Ketul", City = "Vijapur" },
                                            new Person{ Name = "Kisan", City = "Vijapur" },
            };
            var query = from p in persons
                        let lname = p.Name.ToLower()
                        where lname == "kisan"
                        select new { lname, p.City };

            foreach (var name in query)
                Console.WriteLine(name.lname + " - " + name.City);
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }       
    }
    public class Person
    {
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ Queries “into” Keyword
Code:
class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons = new List<Person>{
                                            new Person{ Name = "Kisan", City="Ladol" },
                                            new Person{ Name = "Ravi", City = "Ladol" },
                                            new Person{ Name = "Kate", City = "Vijapur" },
                                            new Person{ Name = "Kisan", City = "Vijapur" },
            };
            var query = from p in persons
                        where p.Name.StartsWith("K")
                        select p
                            into pNames
                            where pNames.Name.Length > 4
                            select pNames;

            foreach (var name in query)
                Console.WriteLine(name.Name + " - " + name.City);
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }       
    }
    public class Person
    {
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ Grouping Operator
Code:
class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = new List<Customer>{
                                            new Customer{ Id = 1, Name = "Kisan", City="Ladol" },
                                            new Customer{ Id = 2, Name = "Kite", City = "Ladol" },
                                            new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" },
                                            new Customer{ Id = 4, Name = "Kisan", City = "Vijapur" },
            };
            var query = from c in customers
                        group c by c.City;
            foreach (var group in query)
            {
                Console.WriteLine("Group City : " + group.Key);
                foreach (var name in group)
                {
                    Console.WriteLine(name.Id + " - " + name.Name);
                }
            }
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ order by Operator
Code:
class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = new List<Customer>{
                                            new Customer{ Id = 1, Name = "Peter", City="Ladol" },
                                            new Customer{ Id = 2, Name = "Kite", City = "Ladol" },
                                            new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" },
                                            new Customer{ Id = 4, Name = "Marry", City = "Vijapur" },
            };
            var query = from c in customers
                        orderby c.Name
                        group c by c.City;
            foreach (var group in query)
            {
                Console.WriteLine("Group City : " + group.Key);
                foreach (var name in group)
                {
                    Console.WriteLine(name.Id + " - " + name.Name);
                }
            }
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ Quantifier Operators : “All” and “Contains”
Code:
class Program
    {
        static void Main(string[] args)
        {
            //LINQ Quantifier Operators : “Any”, “All” and “Contains”
            List<Customer> customers = new List<Customer>{
                                            new Customer{ Id = 1, Name = "Peter", City="Ladol" },
                                            new Customer{ Id = 2, Name = "Kite", City = "Ladol" },
                                            new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" },
                                            new Customer{ Id = 4, Name = "Marry", City = "Vijapur" },
            };
            bool query = customers.All(c => c.Name.ToLower().Contains("p"));
            if (query)
            {
                Console.WriteLine("Existing all customer with name contains p");               
            }                  
            else
            {
                Console.WriteLine("not existing all customer with name contains p. just have Peter");               
            }
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ Quantifier Operators : “All” and “Contains”
Code:
class Program
    {
        static void Main(string[] args)
        {
            //LINQ Quantifier Operators : “Any”, “All” and “Contains”
            List<Customer> customers = new List<Customer>{
                                            new Customer{ Id = 1, Name = "Peter", City="Ladol" },
                                            new Customer{ Id = 2, Name = "Kite", City = "Ladol" },
                                            new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" },
                                            new Customer{ Id = 4, Name = "Marry", City = "Vijapur" },
            };
            bool query = customers.Any(c => c.Name.ToLower().Contains("P"));
            if (query)
            {
                Console.WriteLine("Existing all customer with name contains p");               
            }                  
            else
            {
                Console.WriteLine("not existing all customer with name contains p. just have Peter");               
            }
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ Take Operators
Code:
class Program
    {
        static void Main(string[] args)
        {
            //LINQ Take Operators
            List<Customer> customers = new List<Customer>{
                                            new Customer{ Id = 1, Name = "Peter", City="Ladol" },
                                            new Customer{ Id = 2, Name = "Kite", City = "Ladol" },
                                            new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" },
                                            new Customer{ Id = 4, Name = "Marry", City = "Vijapur" },
            };
            var customer = customers.Take(3);
            foreach (var c in customer)
            {
                Console.WriteLine(c.Name);
            }
           
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ Skip Operators
Code:
class Program
    {
        static void Main(string[] args)
        {
            //LINQ Take Operators
            List<Customer> customers = new List<Customer>{
                                            new Customer{ Id = 1, Name = "Peter", City="Ladol" },
                                            new Customer{ Id = 2, Name = "Kite", City = "Ladol" },
                                            new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" },
                                            new Customer{ Id = 4, Name = "Marry", City = "Vijapur" },
            };
            var customer = customers.Skip(2);
            foreach (var c in customer)
            {
                Console.WriteLine(c.Name);
            }
           
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ Partitioning Operators – “Take” and “Skip”
Code:
class Program
    {
        static void Main(string[] args)
        {
            //LINQ Take Operators
            List<Customer> customers = new List<Customer>{
                                            new Customer{ Id = 1, Name = "Peter", City="Ladol" },
                                            new Customer{ Id = 2, Name = "Kite", City = "Ladol" },
                                            new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" },
                                            new Customer{ Id = 4, Name = "Marry", City = "Vijapur" },
            };
            var customer = customers.Skip(2).Take(2);
            foreach (var c in customer)
            {
                Console.WriteLine(c.Name);
            }
           
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
LINQ Element Operators
Code:
class Program
    {
        static void Main(string[] args)
        {
            //LINQ Take Operators
            List<Customer> customers = new List<Customer>{
                                            new Customer{ Id = 1, Name = "Peter", City="Ladol" },
                                            new Customer{ Id = 2, Name = "Pite", City = "Ladol" },
                                            new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" },
                                            new Customer{ Id = 4, Name = "Marry", City = "Vijapur" },
            };
            var customer = (from c in customers where c.Name.Contains("P") select new { c.Id, c.Name,c.City}).FirstOrDefault();
            Console.WriteLine("Name: " + customer.Name);
            Console.WriteLine("Press any key to continue!!!");
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
Result
Done!!!

0 comments:

Post a Comment