Linq Notes
Linq Notes
LINQ is query language designed by Microsoft in .net3.5 framework designed similar to SQL.
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different
sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming
languages and databases, as well as providing a single querying interface for different types of data sources.
Linq to object
class Program
int[] arr = { 12, 45, 6, 78, 8, 90, 1, 34, 56, 54, 57, 89 };
int count = 0;
count++;
}
}
int index = 0;
barr[index] = arr[i];
index++;
foreach(int i in barr)
Array.Sort(barr);
Console.ReadKey();
}
}
// Using Linq
Console.ReadKey();
}
LINQ to SQL
SQL LINQ
Table Class
Columns Properties
Record Instances
Stored procedure Methods
Step1 :
To work with Linq to Sql first we need to convert all the relations objects of database into object oriented
types and this process is known as ORM (Object Relational Mapping). To perofrm ORM we are provided
with tool known as OR designer.
)
Step1 : Perform ORM by adding OR Designer
1. In project add new item LINQ to SQL classes. It will add dbml file to project (database markup
language). Preferred name is databasename. It will add ORM tool and display two panels in it.
Left side panel for database
Right side panel for stored procedure.
It will add companyDB.designer.cs file which contains designer code writeen by visual studio which
contains CompanyDBcontext class derived from System.Data.Linq.DBContext for compnayDb
database which helps for to connect to the database whenever we create instance of class/
companyDBcontext reads connectionstring from config file and helps you to connect to database by
creating instance of this class. It also contains overloaded constructors.
2. Drag table to which you want to connect in left side pane. Which shows following window in pane.
<connectionStrings>
<add
name="WindowsFormsApplication1.Properties.Settings.CompanyDBC
onnectionString"
connectionString="Data Source=.;Initial
Catalog=CompanyDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
b) It will add 5th constructor to dbml file.
public EmployeeDBDataContext() :
base(global::WindowsFormsApplication1.Properties.Settings.Def
ault.CompanyDBConnectionString, mappingSource)
{
OnCreated();
}
c) It adds property Employees which returns table of Employee.
d) It adds class Employee containing properties for each field of table
In this way table get converted to class, attributes get changed into properties. And at time of execution of
application objects of class get instantiated.
Eno = Convert.ToInt32(textBox1.Text),
Ename = textBox2.Text,
Job = textBox3.Text,
Salary =Convert.ToInt32(textBox4.Text),
Dname = textBox5.Text
};
dc.Employees.InsertOnSubmit(Employees);
dc.SubmitChanges();
}
string connectString =
System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLDBConnectionString"].ToSt
ring();
Employee employee =
db.Employees.FirstOrDefault(e
=>e.Name.Equals("Michael"));
Textbox1.text=employee.Eno;
Textbox2.text=employee.Ename;
string connectString =
System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLDBConnectionString"].ToSt
ring();
LinqToSQLDataContext db = newLinqToSQLDataContext(connectString);
//Delete Employee
db.Employees.DeleteOnSubmit(deleteEmployee);