Introduction:
In this article, i am going to explain how to effectively use linq against sql database,
dataset,xml datasource,object collections.
Main:
LINQ –> Language Integrated Query,
We can define LINQ is a new programming model for data access that integrates query support
directly within the .net languages.
Specifically designed to reduce the complexity when working with data,
Can be used against any collection,
Work with data in a consistent way, regardless of the type of data,
Interact with data as objects,
Better integration with programming languages,
Improved productivity through IntelliSense in Visual Studio,
LINQ Architecture -O/R(Objects -to-relational) mapping,
Linq architecture helps to create object-to-relation mapping within the .Net framework against
sql server databases,
O/R mapping simply turns the tables and rows into classes and objects,
Helps to encoding the attributes into external xml file,
See this below simple demonstration,
LINQ TO SQL:
1.Linq-to-SQL is an Object/relational (O/R) mapping implementation that allows developers and
architectures to model a relation database using .NET classes.
2.Operations on classes are translated to commands for Crate/Read/Update/Delete (CRUD) statements
that modify the underlying database through a regular connection.
3.Link-to-SQL data model is referred as Data Context and created with MicrosftS Visual Studio 2008
– O/R Designer.
4.Data context contain plain containers with data without behavior. i.e Properties w/o methods such
as Load,LoadAll,Save,Validate.
5.Classes in data context marked with partial so that it can support extensions to be included in future.
Developers operate on Data context using LINQ.
Step 1: — Goto File –> New –> Select Asp.Net Webapplication and named it LinqDemoApp,

Ste2 2: — Right Click the solution — and add new item — select Linq to sql classes and named it
empclasses.dbml,

step 3: — Goto Server Explorer — Connect yours datasource — drag and drop the needed table into
here,

Step 4: — Click Save,Open EmpClass.Designer.cs and check the datacontext,
Now we created linq-to-sql data model (data context),
Add new webform,and add one gridview for displaying results,
Copy the below code in page_load event,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LinqDemoApp
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
EmpClassDataContext objcontext = new EmpClassDataContext();
//Linq Code
var gridviewdata = from c in objcontext.Emps
where c.Deptid == 3
select c;
GridView1.DataSource = gridviewdata;
GridView1.DataBind();
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace LinqDemoApp { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { EmpClassDataContext objcontext = new EmpClassDataContext(); //Linq Code var gridviewdata = from c in objcontext.Emps where c.Deptid == 3 select c; GridView1.DataSource = gridviewdata; GridView1.DataBind(); } } } |
Check the linq code i used,
The from keyword is used to looping the collection (Here collection is emp table),
The Where keyword helps to evaluate each object in the collection,
The select keyword helps to retrieve the returned value,

Now i hope you understood linq to sql-data-model,
LINQ to XML:
Linq to xml provides its own model for manipulating xml regardless source,
We don’t need any additional syntax like xSLT,XPATH,
var data = XDocument.Load(@”Emp.xml”);
var selectedemp=from b in data.emp
where b.empid == “5303”
select b;
var data = XDocument.Load(@”Emp.xml”); var selectedemp=from b in data.emp where b.empid == “5303” select b; |
LINQ to DataSet:
Linq-to-dataset allows us to querying over the dataset,and also allows us us to work with
set of datarows.
var empdata = ds.Tables[0].AsEnumerable();
var result = from b in empdata
where b.Table.Rows.Count>0
select b.Table;
GridView1.DataSource = result;
GridView1.DataBind();
var empdata = ds.Tables[0].AsEnumerable(); var result = from b in empdata where b.Table.Rows.Count>0 select b.Table; GridView1.DataSource = result; GridView1.DataBind(); |
LINQ to OBJECT:
Enables to perform complext queries on any .Net type that implements IEnumerable and IQuerable.
Extreme simplification of query process.
int[] fibono=new int[] {0,1,1,2,3,5};
var data = from n in fibono where n %2 == 0 select n;
Response.Write(n);
int[] fibono=new int[] {0,1,1,2,3,5}; var data = from n in fibono where n %2 == 0 select n; Response.Write(n); |
Conclusion:
Hope this helps,
Happy Coding.
Nice post! I like your way to describe this with graphics. Thanks for this post so we can understand it easily. Keep posting.
I want to quote your post in my blog. It can?
And you et an account on Twitter?