Professional Documents
Culture Documents
LINQ (Language Integrated Query) : It Is An Acronym For Language Integrated Query Which Was Released As A A Language
LINQ (Language Integrated Query) : It Is An Acronym For Language Integrated Query Which Was Released As A A Language
LINQ (Language Integrated Query) : It Is An Acronym For Language Integrated Query Which Was Released As A A Language
Basically LINQ address the current database development model in the context of
Object Oriented Programming Model. If some one wants to develop database
application on .Net platform the very simple approach we uses ADO.Net. ADO.Net is
serving as middle ware in application and provides complete object oriented wrapper
around the database SQL.So developer must have good knowledge of object oriented
concept as well as SQL to develop an application. But incase of Linq SQL statements are
become part of the C# and VB.Net code so there are less chance of mistake.
LINQ Architecture
Advantages of LINQ
LINQ can be used for querying multiple data sources such as relational data and
XML data.
LINQ is declarative, it is very easy to understand and maintain.
Var keyword
Var is new data type that has been introduced in 3.5 .Var request a variant DT,
In linq the actual Datatype is set at compile time.
Linq to objects
Place a buuton
Response.Write(a);
Response.Write("<br>");
Output
foreach(int j in output)
{
Response.Write(j.ToString()+"<br>");
//Response.Write(a);
//Response.Write("<br>");
Lamda Expression
In Lamda expression the ‘=>’ is used to separate input variable on the left and
the body of the expression on the right .
Button_Click()
Var name=names.Select(i=>i);
Foreach(string a in name)
Response.write(a);
}
Linq to string
LINQ to Array
Start with J
protected void Button1_Click(object sender, EventArgs e)
{
string[] ary = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec" };
var res = from months in ary
where months.StartsWith("J") select months
// select new { MonthStartWith_J = months };
GridView1.DataSource = res;
GridView1.DataBind();
}
//GridView2.DataSource = names;
//GridView2.DataBind();
//or
LINQ TO INTARRAY
int[] ary1 = new int[] { 10, 27, 35, 40, 50, 11, 23, 25, 39, 22, 36 };
LINQ TO LIST
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
List<Products> listProducts = new List<Products>();
listProducts.Add(new Products { ProductName = "Mobile", Category = "Electronics",
Price = 6000 });
listProducts.Add(new Products { ProductName = "Fridge", Category = "Electronics",
Price = 12000 });
listProducts.Add(new Products { ProductName = "Chair", Category = "HomeNeeds",
Price = 1000 });
listProducts.Add(new Products { ProductName = "Table", Category = "HomeNeeds",
Price = 6000 });
listProducts.Add(new Products { ProductName = "T-Shirt", Category = "Clothes",
Price = 1500 });
listProducts.Add(new Products { ProductName = "Jeans", Category = "Clothes",
Price = 3500 });
Linq To SQL
Drag tables in it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
ds.SubmitChanges();
viewstudent();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
// GridView1.EditIndex = -1;
//GridView1.EditIndex = -1;
//Label1.Text = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
// a = int.Parse(Label1.Text);
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
// Response.Write(id);
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
// Response.Write(name);
//string age = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
// Response.Write(address);
}
}
Display in Gridview
}
protected void Button1_Click(object sender, EventArgs e)
{
var employees = from emps in r.employeenews
select new
{
emps.name,
emps.salary
};
GridView1.DataSource = employees;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
select new
{
emps.eid,
emps.name,
emps.salary
};
GridView2.DataSource = employees;
GridView2.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
var employees = from emps in r.employeenews
join depts in r.departments on emps.did equals depts.did
select new
{
emps.name,
emps.salary,
depts.deptname
};
GridView1.DataSource = employees;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var employees = from emps in r.employeenews
join depts in r.departments on emps.did equals depts.did
select new
{
emps.name,
emps.salary,
depts.deptname
};
GridView1.DataSource = employees;
GridView1.DataBind();
}
}
Assignment
Where ,Group By
Like Operator
public partial class LINQTOSQL_like : System.Web.UI.Page
{
roseDataContext r = new roseDataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//MyDBDataContext sqlObj = new MyDBDataContext();
var employees = from emps in r.employeenews
where emps.name.Contains("j")
select new
{
emps.name,
emps.salary
};
GridView1.DataSource = employees;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var employees = from emps in r.employeenews
where emps.name.StartsWith("j")//.EndsWith(“r”)
select new
{
emps.name,
emps.salary
};
GridView1.DataSource = employees;
GridView1.DataBind();
}
}
Insert,Update in a Table
public partial class LINQTOSQL_Inserttable : System.Web.UI.Page
{
roseDataContext r = new roseDataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
employeenew em = new employeenew();
em.name = "rose";
em.did = 2;
em.salary = "40000";
r.employeenews.InsertOnSubmit(em);
r.SubmitChanges();
}
protected void Button2_Click(object sender, EventArgs e)
{
roseDataContext r = new roseDataContext();
employeenew em =r.employeenews.Single(x => x.eid== 2);
em.name = "Sreeja S Nair";
r.SubmitChanges();
}
}
Single() - There is exactly 1 result, an exception is First() - There is at least one result, an exception is
thrown if no result is returned or more than one thrown if no result is returned.
result.
FirstOrDefault() - Same as First(), but not thrown
SingleOrDefault() – Same as Single(), but it can any exception or return null when there is no result.
handle the null value.
Single() asserts that one and only one element First() simply gives you the first one.
exists in the sequence.
Single() or SingleOrDefault() will generate a regular The First() or FirstOrDefault() method will generate
TSQL like "SELECT ...". the TSQL statment like "SELECT TOP 1..."
In the case of First / FirstOrDefault, only one row is retrieved from the database so it performs slightly
better than single / SingleOrDefault. such a small difference is hardly noticeable but when table contain
large number of column and row, at this time performance is noticeable.