Wednesday, May 23, 2012
Getting started with LINQ
Linq is integrated in Visual studio as Query language.In Visual Studio you can write LINQ queries in Visual Basic or C# with SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that supports IEnumerable or the generic IEnumerable
The only requirement is that the project target .NET Framework 3.5 or later.
Lets get started with sample class implemented to explain LINQ.
Before we go straight into LINQ, there are couple of other features like Predicate Expression,delegates,Lambda expressions are explained in the same code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQPractice
{
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public double Salary { get; set; }
public string DeptName { get; set; }
}
class Program
{
static void Main(string[] args)
{
Employee[] employees = new Employee[]
{
new Employee{EmpId=100,EmpName="sachin",Salary=15000},
new Employee{EmpId= 101,EmpName="scott",Salary=12000},
new Employee{EmpId=102,EmpName="rahul",Salary=13000},
new Employee{EmpId=103,EmpName="ilyas",Salary=14000}
};
Console.WriteLine("Display using predicate expression");
Console.WriteLine("----------------------------------");
Employee sachin = Array.Find(employees, FindEmployeePredicate);
Console.WriteLine("{0,-5}{1,-8}{2,-6}",sachin.EmpId,sachin.EmpName ,sachin.Salary );
Console.ReadLine();
Console.WriteLine("Display using Delegates");
Console.WriteLine("----------------------------------");
Employee scott = Array.Find(employees,
delegate(Employee emp)
{
return emp.EmpName == "scott";
});
Console.WriteLine("{0,-5}{1,-8}{2,-6}", scott.EmpId, scott.EmpName, scott.Salary);
Console.ReadLine();
Console.WriteLine("Display using Lambda expression");
Console.WriteLine("----------------------------------");
Employee rahul = Array.Find(employees,e=>e.EmpName =="rahul");
Console.WriteLine("{0,-5}{1,-8}{2,-6}", rahul.EmpId, rahul.EmpName, rahul.Salary);
Console.ReadLine();
Console.WriteLine("--------LINQ IMPLEMENTATION-------");
Console.WriteLine("Displaying single row query result");
Console.WriteLine("----------------------------------");
Employee ilyas = (from emp in employees
where emp.EmpName=="ilyas"
select emp).Single();
Console.WriteLine("{0,-5}{1,-8}{2,-6}", ilyas.EmpId, ilyas.EmpName, ilyas.Salary);
Console.ReadLine();
Console.WriteLine("Displaying list of rows query result");
Console.WriteLine("----------------------------------");
Employee[] list = (from emp in employees
select emp).ToArray();
foreach (var item in list)
{
Console.WriteLine("{0,-5}{1,-8}{2,-6}", item.EmpId, item.EmpName, item.Salary);
}
Console.ReadLine();
Console.WriteLine("Displaying query result -- with Order by");
Console.WriteLine("----------------------------------");
Employee[] list1 = (from emp in employees
orderby emp.EmpName
select emp).ToArray();
foreach (var item in list1)
{
Console.WriteLine("{0,-5}{1,-8}{2,-6}", item.EmpId, item.EmpName, item.Salary);
}
Console.ReadLine();
Console.WriteLine("Displaying specific columns of query result");
Console.WriteLine("----------------------------------");
var list2 = (from emp in employees
orderby emp.EmpName
select new {Name = emp.EmpName,Salary = emp.Salary});
foreach (var item in list2)
{
Console.WriteLine("{0,-8}{1,-6}", item.Name, item.Salary);
}
Console.ReadLine();
}
static bool FindEmployeePredicate(Employee emp)
{
return emp.EmpName == "sachin";
}
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment