Monday, February 13, 2012

LINQ and Lambda expression


Hi All,
This is a simple application showing how to use LINQ and Lambda expression 

Step 1:
Create a simple Customer Class:
  public class Customer
        {
            public int Id;public string Name;public string City;
            public Customer(int id, string name, string city)
            {
                Id = id;Name = name;City = city;
            }
        }

Step 2:
Add few customers to the class as below:
List<Customer> customers = new List<Customer>();
            customers.Add(new Customer(1, "Dave", "Sarasota"));
            customers.Add(new Customer(2, "John", "Tampa"));
            customers.Add(new Customer(3, "Abe", "Miami"));

Next we try to write some test method which gets required customer on some condition.
Let’s name it as getCust

Sorry I am not following any naming conventions here J ……


The method looks as below:
This method tries to fetch the result from the list using the below:
·         FirstOtDefault
·         FindAll
·         Find
·         Where
·         select




public static Customer getCust(List<Customer> lcust)
        {
            Customer objcust = null;

            //using simple FirstOtDefault
            objcust = lcust.FirstOrDefault(c => c.Id == 1);

            //Using FindAll
            objcust = lcust.FindAll(c => c.Id == 1).FirstOrDefault();

            //using Predicate and using Find
            Predicate<Customer> pred = item => item.Id == 2;
            objcust = lcust.Find(pred);

            //using Func for Where
            Func<Customer, bool> whereFunc = delegate(Customer cutitemem)
            {
                return cutitemem.Id == 3;
            };

            objcust = lcust.Where(whereFunc).FirstOrDefault();

            //Using simple  LINQ
            var result = from c in lcust
                         where c.Id.Equals(1)
                         select c;
            objcust = result.ToList<Customer>().FirstOrDefault();

            return objcust;
        }
       
 
As you can see the above method shows  different techniques of  using LINQ and Lambda.

ok.. here is the full code :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleLINQ_Lamda
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer(1, "Dave", "Sarasota"));
            customers.Add(new Customer(2, "John", "Tampa"));
            customers.Add(new Customer(3, "Abe", "Miami"));
            Customer abe = getCust(customers);
        }

        public static Customer getCust(List<Customer> lcust)
        {
            Customer objcust = null;
            //using simple FirstOtDefault
            objcust = lcust.FirstOrDefault(c => c.Id == 1);

            //Using FindAll
            objcust = lcust.FindAll(c => c.Id == 1).FirstOrDefault();

            //using Predicate and using Find
            Predicate<Customer> pred = item => item.Id == 2;
            objcust = lcust.Find(pred);

            //using Func for Where
            Func<Customer, bool> whereFunc = delegate(Customer cutitemem)
            {
                return cutitemem.Id == 3;
            };

            objcust = lcust.Where(whereFunc).FirstOrDefault();

            //Using simple  LINQ
            var result = from c in lcust
                         where c.Id.Equals(1)
                         select c;
            objcust = result.ToList<Customer>().FirstOrDefault();

            return objcust;
        }
       
        public class Customer
        {
            public int Id;public string Name;public string City;
            public Customer(int id, string name, string city)
            {
                Id = id;Name = name;City = city;
            }
        }
    }
}

Guys pls do comment n give suggestions on the post .. Thanks in adv

0 comments:

Post a Comment