Basic Of LINQ

Example of LINQ
  1. using System;  
  2. using System.Linq;  
  3. class Program {  
  4.     static void Main() {  
  5.         string[] words = {  
  6.             "hello",  
  7.             "wonderful",  
  8.             "LINQ",  
  9.             "beautiful",  
  10.             "world"  
  11.         };  
  12.         var shortWords = from word in words  
  13.         where word.Length <= 5  
  14.         select word;  
  15.         foreach(var word in shortWords) {  
  16.             Console.WriteLine(word);  
  17.         }  
  18.         Console.ReadLine();  
  19.     }  
  20. }  
Output

hello
LINQ
world
The types of LINQ

LINQ to Objects
LINQ to XML(XLINQ)
LINQ to DataSet
LINQ to SQL (DLINQ)
LINQ to Entities
LINQ to SQL Example
  1. using System;  
  2. using System.Linq;  
  3. namespace LINQtoSQL {  
  4.     class LinqToSQLCRUD {  
  5.         static void Main(string[] args) {  
  6.             string connectString = System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLDBConnectionString"].ToString();  
  7.             LinqToSQLDataContext db = new LinqToSQLDataContext(connectString);  
  8.             //Create new Employee  
  9.             Employee newEmployee = new Employee();  
  10.             newEmployee.Name = "ABC";  
  11.             newEmployee.Email = "[email protected]";  
  12.             newEmployee.ContactNo = "1234567890";  
  13.             newEmployee.DepartmentId = 3;  
  14.             newEmployee.Address = "India";  
  15.             //Add new Employee to database  
  16.             db.Employees.InsertOnSubmit(newEmployee);  
  17.             //Save changes to Database.  
  18.             db.SubmitChanges();  
  19.             //Get new Inserted Employee  
  20.             Employee insertedEmployee = db.Employees.FirstOrDefault(e => e.Name.Equals("Michael"));  
  21.             Console.WriteLine("Employee Id = {0} , Name = {1}, Email = {2}, ContactNo = {3}, Address = {4}", insertedEmployee.EmployeeId, insertedEmployee.Name, insertedEmployee.Email, insertedEmployee.ContactNo, insertedEmployee.Address);  
  22.             Console.WriteLine("\nPress any key to continue.");  
  23.             Console.ReadKey();  
  24.         }  
  25.     }  
  26. }  
  27. using System;  
  28. using System.Linq;  
  29. namespace LINQtoSQL {  
  30.     class LinqToSQLCRUD {  
  31.         static void Main(string[] args) {  
  32.             string connectString = System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLDBConnectionString"].ToString();  
  33.             LinqToSQLDataContext db = new LinqToSQLDataContext(connectString);  
  34.             //Get Employee for update  
  35.             Employee employee = db.Employees.FirstOrDefault(e => e.Name.Equals("Michael"));  
  36.             newEmployee.Name = "ABC";  
  37.             newEmployee.Email = "[email protected]";  
  38.             newEmployee.ContactNo = "9954567890";  
  39.             newEmployee.DepartmentId = 3;  
  40.             newEmployee.Address = "India-Delhi";  
  41.             //Save changes to Database.  
  42.             db.SubmitChanges();  
  43.             //Get Updated Employee  
  44.             Employee updatedEmployee = db.Employees.FirstOrDefault(e => e.Name.Equals("George Michael"));  
  45.             Console.WriteLine("Employee Id = {0} , Name = {1}, Email = {2}, ContactNo = {3}, Address = {4}", updatedEmployee.EmployeeId, updatedEmployee.Name, updatedEmployee.Email, updatedEmployee.ContactNo, updatedEmployee.Address);  
  46.             Console.WriteLine("\nPress any key to continue.");  
  47.             Console.ReadKey();  
  48.         }  
  49.     }  
  50. }  
  51. using System;  
  52. using System.Linq;  
  53. namespace LINQtoSQL {  
  54.     class LinqToSQLCRUD {  
  55.         static void Main(string[] args) {  
  56.             string connectString = System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLDBConnectionString"].ToString();  
  57.             LinqToSQLDataContext db = newLinqToSQLDataContext(connectString);  
  58.             //Get Employee to Delete  
  59.             Employee deleteEmployee = db.Employees.FirstOrDefault(e => e.Name.Equals("ABC"));  
  60.             //Delete Employee  
  61.             db.Employees.DeleteOnSubmit(deleteEmployee);  
  62.             //Save changes to Database.  
  63.             db.SubmitChanges();  
  64.             //Get All Employee from Database  
  65.             var employeeList = db.Employees;  
  66.             foreach(Employee employee in employeeList) {  
  67.                 Console.WriteLine("Employee Id = {0} , Name = {1}, Email = {2}, ContactNo = {3}", employee.EmployeeId, employee.Name, employee.Email, employee.ContactNo);  
  68.             }  
  69.             Console.WriteLine("\nPress any key to continue.");  
  70.             Console.ReadKey();  
  71.         }  
  72.     }  
  73. }  
LINQ to Object Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace LINQtoObjects {  
  6.     class Program {  
  7.         static void Main(string[] args) {  
  8.             string[] tools = {  
  9.                 "A",  
  10.                 "B",  
  11.                 "C",  
  12.                 "D",  
  13.                 "E",  
  14.                 "F"  
  15.             };  
  16.             var list = from t in tools  
  17.             select t;  
  18.             StringBuilder sb = new StringBuilder();  
  19.             foreach(string s in list) {  
  20.                 sb.Append(s + Environment.NewLine);  
  21.             }  
  22.             Console.WriteLine(sb.ToString(), "Tools");  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  
LINQ to XML Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Xml.Linq;  
  5. namespace LINQtoXML {  
  6.     class ExampleOfXML {  
  7.         static void Main(string[] args) {  
  8.             string myXML = @ "<Departments> < Department > Account < /Department> < Department > IT < /Department> < Department > Sales < /Department> < Department > Marketing < /Department> < /Departments>";  
  9.             XDocument xdoc = new XDocument();  
  10.             xdoc = XDocument.Parse(myXML);  
  11.             var result = xdoc.Element("Departments").Descendants();  
  12.             foreach(XElement item in result) {  
  13.                 Console.WriteLine("Department Name - " + item.Value);  
  14.             }  
  15.             Console.WriteLine("\nPress any key to continue.");  
  16.             Console.ReadKey();  
  17.         }  
  18.     }