ASP.NET LINQ Tutorial
Most applications are data-centric and most of these data repositories are however, relational databases. Over the years designers and developers have designed applications based on object models.
These objects were responsible for connecting to the data access components - called the data access layer (DAL). There are however, two things to consider:
- All the data needed in an application are not stored in the same source. The source could be a relation database, some business objects, XML file, or on web services.
- Accessing in-memory object is simpler and less expensive than accessing data from a database or XML file.
- The data accessed are not used directly, but needs to be sorted, ordered, grouped, altered etc.
So if there is one tool that makes all kinds of data access easy, that allows joining data from such disparate data sources and perform standard data processing operations, in few lines of codes, it would be of great help.
LINQ or Language-Integrated Query is such a tool. LINQ is set of extensions to the .Net Framework 3.5 and its managed languages that sets the query as an object. It defines a common syntax and a programming model to query different types of data using a common language.
The relational operators like Select, Project, Join, Group, Partition, Set operations etc., are implemented in LINQ and the C# and VB compilers in the .Net framework 3.5, which support the LINQ syntax makes it possible to work with a configured data store without resorting to ADO.Net.
For example querying the Customers table in the Northwind database, using LINQ query in C#, the code would be:
var data = from c in dataContext.Customers
where c.Country == "Spain"
select c;
|
Where:
- The 'from' keyword logically loops through the contents of the collection.
- The expression with the 'where' keyword is evaluated for each object in the collection.
- The 'select' statement selects the evaluated object to add to the list being returned.
- The 'var' keyword is for variable declaration. Since the exact type of the returned object is not known, it indicates that the information will be inferred dynamically.
LINQ query can be applied to any data-bearing class that inherits from IEnumerable<T>, here T is any data type, for example List<Book>...
No comments:
Post a Comment