LINQ stands for “Language Integrated Query.” LINQ fundamentally is about integrating query operations into the .NET platform in a comprehensive and open manner. It’s also about providing a unified way for you to query across any kind of data that you have in your program, whether it’s relational, objects or XML.
LINQ has three major components:
- LINQ to Objects
- LINQ to ADO.NET
- LINQ to XML
LINQ to Objects deals with in-memory data. Any class that implements the IEnumerable
LINQ to ADO.NET deals with data from external sources, basically anything ADO.NET can connect to. Any class that implements IEnumerable
LINQ to XML is a comprehensive API for in-memory XML programming
A Simple LINQ Query
First we will create an Employee Class, then we make few instance of it and load our test data.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinqExample { class EmployeeInfo { public EmployeeInfo() {} public String Name { get; set; } public int EmpID { get; set; } public String EMailID { get; set; } public String Address { get; set; } } class Program { public void LinqExample() { // Sample Input. var Emp = new List{ new EmployeeInfo { Name = "Raja", EmpID=1001, EMailID ="rajasingh.s@gmail.com", Address="Chennai" }, new EmployeeInfo{ Name = "Sam", EmpID=1002, EMailID ="ABCD@gmail.com", Address="Chennai" }, new EmployeeInfo{ Name = "Singh", EmpID=1003, EMailID ="ABCD@gmail.com", Address="Chennai" } }; //Linq Query and Result. var oResult = from oEmp in Emp where oEmp.EmpID == 1001 || oEmp.EmpID==1003 select oEmp.Name; //Display Result foreach(var oData in oResult) Console.WriteLine("Emp Name : " + oData); } static void Main(string[] args) { Program pgm = new Program(); pgm.LinqExample(); Console.ReadLine(); } } }
Linq Query
var oResult = from oEmp in Emp where oEmp.EmpID == 1001 || oEmp.EmpID==1003 select oEmp.Name;
Output
Ordering Data
var oResult = from oEmp in Emp orderby oEmp.Name ascending select oEmp.Name;
Joining two List
class Department { public Department() {} public int EmpID { get; set; } public String DeparName { get; set; } } ........ ........ var dep = new List{ new Department{ EmpID=1001, DeparName="SSW" }, new Department{ EmpID=1002, DeparName="HR" }, new Department{ EmpID=1003, DeparName="Finance" } }; var oResult = from oEmp in Emp join oDept in dep on oEmp.EmpID equals oDept.EmpID orderby oEmp.Name select new { oEmp.Name, oEmp.EmpID, oDept.DeparName }; //Display Result foreach (var oData in oResult) { Console.WriteLine("Emp Name : " + oData.Name); Console.WriteLine("Emp ID : " + oData.EmpID); Console.WriteLine("Dept Name : " + oData.DeparName); Console.WriteLine("---------------------------------------------"); }
Output
Grouping
var dep = new List{ new Department{ EmpID=1001, DeparName="SSW" }, new Department{ EmpID=1002, DeparName="HR" }, new Department{ EmpID=1003, DeparName="SSW" } }; var oResult = from oEmp in Emp join oDept in dep on oEmp.EmpID equals oDept.EmpID group oDept by oDept.DeparName; /*select new { oEmp.Name, oEmp.EmpID, oDept.DeparName };*/ //Display Result foreach (var oData in oResult) { Console.WriteLine("No of item in Group : " + oData.Count().ToString()); foreach (var oItem in oData) { Console.WriteLine(" Emp Name : " + oItem.DeparName); Console.WriteLine(" Emp ID : " + oItem.EmpID); } Console.WriteLine("---------------------------------------------"); }
Enjoy the new .NET feature.
Thank You
No comments:
Post a Comment