Beginning LINQ – Part I
LINQ(Language Integrated Query) is one of the features of .NET 3.5. linq avoids the use of traditional sql queries in programming. It follows object oriented approach and linq Is type safe. Any object of type IEnumerable can be used as a datasource to perform linq operations.
There are two approaches in using linq
· Linq Query: This is more flexible and resembles sql queries.
· Linq Methods: Here methods from linq api are used to query a datasource.
In both the case, during execution they are converted back to sql queries to communicate with the datasource. This can be seen by placing a break point near the linq query.
In this article I’ll be dealing with Linq Methods.
To deal with database you should give reference to “System.Data.Linq” namespace.
When you add “.dbml” file to a project it automatically adds reference to set of namespace required for communicating with the database and to use Linq methods in project.
Once you add “.dbml” file drag and drop the tables needed for your project from server explorer. This will creates class files for each table and also add relationship for the table if exists. The relationship is two way (parent table can be accessed via child table and vice versa ). This feature adds to the ease of development.
Dbml file creates a datacontext class in designer.cs file which is important in establishing connection with the database.
Datacontext class is the key class required for committing all the changes back to database. Each table data can be accessed by creating an object of datacontext class.
Other than Datacontext in designer.cs file, there exists other classes which corresponds to tables added to dbml file and this class file contains properties corresponding to fields in table. Properties are of datatype similar to fields in table.
Here is a picture mapping to categories table in northwnd database and displaying its field as properties. Each properties map to corresponding fields in table. Attributes specified at the top of class and properties are responsible for mapping.
Now create an object of datacontext class in your project. Using this object we can access the table objects of respective tables which contains table data and linq methods are applied on this objects for selecting data based on conditions if any.
Note: Throughout this article I’ll be using Microsoft’s famous sample database “NorthWnd.dbo”. if you don’t have the one you can download it from here.
In the above code sample I have created a datacontext object and am using the categories table data for binding it to repeater which will display all the data from categories table.
The intellisense shows the tables that are present in the dbml file. These tables can be directly accesed through datacontext object.
In the page_load event am just binding all the data from categories to repeater.
In the categoryselect() method am using select method of linq to select only required field to display data. Here, in this case it is categoryname and description. You can question what is “=>” next to ‘c’ and why am using “new” keyword inside select method. “=>” symbol represents lambda expression similar to dynamic delegate and “new” is because am selecting it into a new table with only 2 fields. It creates a virtual table. The return type is IEnumerable or IQueriable.



No comments:
Post a Comment