Friday, November 14, 2008

LINQ To Objects

1.Simple Select Query
We can use Language Integrated Query in IEnumerable or IEnumerable(T) Collections.
Sample Code
string employeeName = (from emp in employee.GetEmployees
where emp.EmployeeId == Convert.ToInt32(txtManager.Text
select emp.EmployeeName).FirstOrDefault() as string
//Instead of string employeeName we can use var employeeName//

In the above code employee.GetEmployees() is a Generic list of Employee Class.

2.Join Two Collections
We can join two collections by using Join in LINQ just like INNER JOIN in SQL.
Sample Code
var employeeName = (from emp in employee.GetEmployees()
join manager in managers.GetManagers()
on emp.EmployeeId equals manager.EmployeeId
select new { emp.EmployeeId, emp.EmployeeName, manager.Designation });


Post a Comment