Basic of LINQ

using System;
using System.Linq;
class Program {
static void Main() {
string[] words = {
“hello”,
“wonderful”,
“LINQ”,
“beautiful”,
“world”
};
var shortWords = from word in words
where word.Length e.Name.Equals(“Michael”));
Console.WriteLine(“Employee Id = {0} , Name = {1}, Email = {2}, ContactNo = {3}, Address = {4}”, insertedEmployee.EmployeeId, insertedEmployee.Name, insertedEmployee.Email, insertedEmployee.ContactNo, insertedEmployee.Address);
Console.WriteLine(“\nPress any key to continue.”);
Console.ReadKey();
}
}
}
using System;
using System.Linq;
namespace LINQtoSQL {
class LinqToSQLCRUD {
static void Main(string[] args) {
string connectString = System.Configuration.ConfigurationManager.ConnectionStrings[“LinqToSQLDBConnectionString”].ToString();
LinqToSQLDataContext db = new LinqToSQLDataContext(connectString);
//Get Employee for update
Employee employee = db.Employees.FirstOrDefault(e => e.Name.Equals(“Michael”));
newEmployee.Name = “ABC”;
newEmployee.Email = “abc@xyz.com”;
newEmployee.ContactNo = “9954567890”;
newEmployee.DepartmentId = 3;
newEmployee.Address = “India-Delhi”;
//Save changes to Database.
db.SubmitChanges();
//Get Updated Employee
Employee updatedEmployee = db.Employees.FirstOrDefault(e => e.Name.Equals(“George Michael”));
Console.WriteLine(“Employee Id = {0} , Name = {1}, Email = {2}, ContactNo = {3}, Address = {4}”, updatedEmployee.EmployeeId, updatedEmployee.Name, updatedEmployee.Email, updatedEmployee.ContactNo, updatedEmployee.Address);
Console.WriteLine(“\nPress any key to continue.”);
Console.ReadKey();
}
}
}
using System;
using System.Linq;
namespace LINQtoSQL {
class LinqToSQLCRUD {
static void Main(string[] args) {
string connectString = System.Configuration.ConfigurationManager.ConnectionStrings[“LinqToSQLDBConnectionString”].ToString();
LinqToSQLDataContext db = newLinqToSQLDataContext(connectString);
//Get Employee to Delete
Employee deleteEmployee = db.Employees.FirstOrDefault(e => e.Name.Equals(“ABC”));
//Delete Employee
db.Employees.DeleteOnSubmit(deleteEmployee);
//Save changes to Database.
db.SubmitChanges();
//Get All Employee from Database
var employeeList = db.Employees;
foreach(Employee employee in employeeList) {
Console.WriteLine(“Employee Id = {0} , Name = {1}, Email = {2}, ContactNo = {3}”, employee.EmployeeId, employee.Name, employee.Email, employee.ContactNo);
}
Console.WriteLine(“\nPress any key to continue.”);
Console.ReadKey();
}
}
}
LINQ to Object Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQtoObjects {
class Program {
static void Main(string[] args) {
string[] tools = {
“A”,
“B”,
“C”,
“D”,
“E”,
“F”
};
var list = from t in tools
select t;
StringBuilder sb = new StringBuilder();
foreach(string s in list) {
sb.Append(s + Environment.NewLine);
}
Console.WriteLine(sb.ToString(), “Tools”);
Console.ReadLine();
}
}
}
LINQ to XML Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace LINQtoXML {
class ExampleOfXML {
static void Main(string[] args) {
string myXML = @ ” Account IT Sales Marketing “;
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
var result = xdoc.Element(“Departments”).Descendants();
foreach(XElement item in result) {
Console.WriteLine(“Department Name – ” + item.Value);
}
Console.WriteLine(“\nPress any key to continue.”);
Console.ReadKey();
}
}
}

Advertisements