LINQ Basic Queries

In this Blog we learn Basic LINQ Queries in Asp.Net .

       1- Fill Gridview record:

public void showgridview()
{
DataClassesDataContext dc = new DataClassesDataContext();
var q =
from a in dc.GetTable<bio>()
select a;
GridView1.DataSource = q;
GridView1.DataBind();
}


      2- Search record:

public void SearchQuery()
{
DataClassesDataContext dc = new DataClassesDataContext();
var q =
from a in dc.GetTable<bio>()
where a.name == TextBox3.Text
select a;
GridView1.DataSource = q;
GridView1.DataBind();
}


      3- Insert Record

Public void InsertQuery()
{
DataClassesDataContext dc = new DataClassesDataContext();
bio obj = new bio();
obj.name = TextBox3.Text;
obj.passw = TextBox5.Text;
obj.fname = TextBox4.Text;
dc.bios.InsertOnSubmit(obj);
dc.SubmitChanges();
showgridview();
}


      4- Update record

public void UpdateQuery()
{
DataClassesDataContext dc = new DataClassesDataContext();
bio objbio = dc.bios.Single(bio => bio.name == TextBox3.Text);
objbio.passw = TextBox5.Text;
objbio.fname = TextBox4.Text;
dc.SubmitChanges();
}


      5- Delete Record

public void DeleteQuery()
{
DataClassesDataContext dc = new DataClassesDataContext();
bio obb = dc.bios.Single(bio => bio.name == TextBox3.Text);
dc.bios.DeleteOnSubmit(obb);
dc.SubmitChanges();
}


      6- Inner Join

There is two table fee and bio.

Bio Table:


Fee Table:

DataClassesDataContext dc = new DataClassesDataContext();
var biofee = from bio in dc.bios
join fee in dc.fees
on bio.id equals fee.id
select new { bio.name, bio.fname, fee.Address };
GridView1.DataSource = biofee;
GridView1.DataBind();
//You can also do this with the help of lambda operator
// Inner joinning Usin lambda operator
DataClassesDataContext dc = new DataClassesDataContext();
var query = dc.bios.Join(dc.fees, r => r.id, p => p.id, (r, p) => new { r.name, r.fname, p.Address });
GridView1.DataSource = query;
GridView1.DataBind();


Next Recommended Reading LINQ : Standard Query Operators