Friday 6 January 2012

Simple LINQ to Objects example


List<Employee> employees = new List<Employee>()
            {
                new Employee { ID=1, Name="Ravi", JoinDate=new DateTime(2002, 1, 15) },
                new Employee { ID=2, Name="Raja", JoinDate=new DateTime(2002, 3, 17 },
                new Employee { ID=3, Name="Mani", JoinDate=new DateTime(2007, 2, 11) }
            };
 
            IEnumerable<Employee> query =
                from e in employees
                where e.JoinDate.Year < 2003
                orderby e.Name
                select e;
 
            foreach (Employee e in query)
            {
                Console.WriteLine(e.Name);
            }
 
 
Employee Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace W3mentorLinq
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime JoinDate { get; set; }
    }
}

No comments:

Post a Comment