Friday, 19 August 2016

 

Basic of LINQ

 

The acronym LINQ is for Language Integrated Query. Microsoft’s query language is fully integrated and offers easy data access from in-memory objects, databases, XML documents and many more. It is through a set of extensions, LINQ ably integrate queries in C# and Visual Basic.

 

Basic Example 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 <= 5
select word;
foreach (var word in shortWords)
{
Console.WriteLine(word);
}
Console.ReadLine();
}
}
 
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
 
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);
//Create new Employee
Employee newEmployee = new Employee();
newEmployee.Name = "ABC";
newEmployee.Email = "abc@xyz.com";
newEmployee.ContactNo = "1234567890";
newEmployee.DepartmentId = 3;
newEmployee.Address = "India";
//Add new Employee to database
db.Employees.InsertOnSubmit(newEmployee);
//Save changes to Database.
db.SubmitChanges();
//Get new Inserted Employee
Employee insertedEmployee = db.Employees.FirstOrDefault(e =>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 = @"<Departments>
<Department>Account</Department>
<Department>IT</Department>
<Department>Sales</Department>
<Department>Marketing</Department>
</Departments>";
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();
}
}
}

 

No comments :