s
Contact Login Register
h M

How to Make LINQ Work For You in ASP.NET

LINQ TO SQL, LINQ TO XML

Author: Chad Nash/Thursday, October 10, 2013/Categories: SQL Server Visual Studio Articles

Rate this article:
5.0
How to Make LINQ Work For You in ASP.Net
For many programmers LINQ (Language Integrated Query) is the single most important innovation in .Net 3.5. It has radically altered the way programmers view and treat data.
 
Before LINQ was introduced .Net was quite like any other programming framework, providing a set of classes and controls that one could use to create applications with lesser difficulty and complexity. It could be compared to MFC and we wouldn't be very far off. But .Net framework 2 revolutionized the way people look at .Net and after .Net framework 3.5 it has become very hard to put .Net in the same box as any other framework, even Java.
 
The path to the development of LINQ was set with the introduction of generic types in framework 2. Generic Types made it possible to define a common set of operations that could work with all data and Microsoft used them to build first Lambda expressions and then an SQL like query syntax that made working with in memory data much more intuitive.
 
LINQ works with almost every kind of data including collections, XML, and even databases. Microsoft wanted to make LINQ as universal as possible, that's why it has emphasized the use of Generic collections and has introduced projects like Linq-To-Sql which  connects LINQ queries directly to SQL Server.
Before we dive into LINQ code, let's have a quick look at:
 
What LINQ can work with.
1. Linq-To-Objects > A part of .Net Framework 3 it makes LINQ a first class citizen of .Net Framework. You can use LINQ from directly within .Net to query/modify/update any collection that's implements IEnumerable<T> (all generic collections). This gives you SQL like filtering, sorting and updating capabilities with your variables right inside the language. It will work with any object that implements the IEnumerable<T> interface.
 
2. Linq-To-XML > It extends the System.Xml namespace to provide fresh extension methods and classes that you can use to make working with XML data easier.
 
3. Linq-To-SQL > Since LINQ provides a powerful set of tools for working with data collections, it is only natural that users of LINQ would like to use it with databases since there's no point in learning SQL when they already know LINQ. To gives programmers a uniform method to access all kinds of data Microsoft introduced Linq-To-Sql which lets you work with a SQL Server database as if it was an in-memory variable. Of course, now Linq-To-Sql is not the only interface to databases.
Many other database frameworks like Entity Framework, NHibernate, ADO.Net and others have support for LINQ queries. Third party LINQ providers have brought LINQ queries to every leading database format. Some like NHibernate are free, while others are sold as components.
 
How does LINQ work
I've already said earlier that LINQ wasn't a stand-alone effort. It is built upon some important feature additions in .Net framework: extension methods, lambda expressions and expression trees.
 
Extension methods lets a programmer extend the functionality of any class (sealed or otherwise) by adding static methods that work on the class object.
 
Lambda Expressions are a compact way to create anonymous functions. Many LINQ functions accept Lambda functions are arguments for filtering and selection queries. If you are new to Lambda, you'll find the syntax a bit queer and it will take a little getting used to. Here's a sample Lambda expression:
 
mydelegate multi = (x, y) => x * y;
 
This simple Lambda expression creates an anonymous function which takes two parameters and multiplies them.
 
The Lambda expressions are used to create Expression trees which are the soul of LINQ. Think of Expression Trees as a way to store C# code as data. This function-data is re-compiled (or interpreted if you like) to code which can be run on the collection you've sent to LINQ.
 
Though the LINQ query syntax is much more verbose than Lambda expressions, they are converted into Lambda expressions by the compiler.
 
Enough of background theory already! Let's see some real-world practical application of LINQ.
 
LINQ in ASP.Net
ASP.Net utilizes the full language features of .Net so you can put LINQ to good use in your ASP.Net projects. Using LINQ you can work with your .Net collections to filter and sort data effectively. Let's look at a sample query:
 
        struct Contact
        {
            public string Name;
            public string Phone;
            public string Age;
            public string ID;
 
        }
 
        public List<Contact> FilterContactsByName(string filter)
        {
            List<Contact> contacts = GetMyContacts();
 
            var res = from cn in contacts
                      where cn.Name.StartsWith(filter)
                      select cn;
 
            return res.ToList<Contact>();
        }
 
We have created a struct Contact with the fields -- Name, Phone, Age and ID. The function FilterContactsByName gets all the contacts that start with the characters in the argument ex. 'ad' will bring all people that have names beginning with 'ad' like Adrienne.
 
Notice that the LINQ query looks very much like a SQL query, but the select is at the end instead of the beginning. All LINQ queries begin with from. The first variable cn is a temporary variable that we will use to specify LINQ conditions against. Think of it like the temporary variable in a C# foreach loop.
 
The next clause is 'where'. Where is used to specify any filtering conditions that we may have. If you want, you can have simple queries without where. In the where clause we are using the 'StartsWith' extension method on the Name property of cn to find out if the name starts with certain characters.
 
The third part -- select is used to return all elements that match the where condition.
 
 Notice that the variable that holds the result is of type 'var'. Here's another new concept of .Net Framework 3.5 which is important to LINQ -- type inference.
 
Using type inference a variable needn't be declared with any specific type in mind. Instead it implicitly takes the type of the object that results from the operation. So you can say that type inference passes the type information along with the data which is a very powerful technique and it frees you up to retrieve data flexibly without worrying about the data type.
 
Custom Types
Since the type of the data retrieved is inferred (var is not a variant or an object), there is full intellisense support and you can run all data operators on it. The variable frees you up to retrieve data of any type. We could even return a custom type created on the fly using object initialization. Here's an example:
 
            var res = from cn in contacts
                      where cn.Name.StartsWith(filter)
                      select new { cn.Name, cn.Age };
 
In this example we are only retrieving the age and the name in a custom type, the variable res will still have intellisense support.
 
Aggregate Functions
Since the LINQ query syntax is based on SQL it aims to provide as many features of SQL that it can. Aggregate functions like Count, Sum, Average, Max, Min etc., are fully supported in LINQ. So if you want to get any aggregation information from the dataset you can do so easily. Check out this example:
 
            var res = (from cn in contacts
                      where cn.Name.StartsWith(filter)
                      select cn).Count();
 
This will return the count of all items that begin with the letters we've passed.
 
LINQ Query Operators
LINQ supports most of the common query operators supported by SQL, like:
 
SELECT - Select a result set.
OrderBy - Sort the result set.
Take(n) - Limit the result set to the specified row-count.
Skip(n) - Skip n records before selecting. Can be used together with Take to implement paging.
GroupBy - Grouping the resultset on a criteria.
Etc...
 
Here's a slightly complex query which makes use of some of these functions.
 
            var res2 = (from cn in contacts
                        orderby cn.Name
                        select cn).GroupBy(cn => cn.Age).Skip(2).Take(100);
 
As you can see some of the query operators can be used in the query syntax, while others must be used as extension methods.
 
In this particular query we are selecting all contacts grouped by age and sorted by name; we are also using Skip and Take to start selection after the first 2 records and select only the subsequent 100.
 
Joins in LINQ
Joining two result-set is a very fundamental and important task while working with databases. LINQ makes that possible too, and not just with databases but with any in-memory collection that implements IEnumerable. Here's an example join in LINQ:
 
        public struct Contact
        {
            public string Name;
            public string Phone;
            public int Age;
            public int ID;
        }
 
        public struct EmailRecord
        {
            public DateTime Created;
            public string Text;
            public int contactID;
        }
 
       var res3 = from cn in contacts
                       join eml in emails on
                       cn.ID equals eml.contactID
                       select new { cn.Name, eml.Text };
 
This example has a simple join between two tables . We are retrieving email text sent by a user. Notice that we are using the 'equals' LINQ operator and not '==' to compare the equality of the values.
 
Endnote
With the introduction of LINQ Data is a first class citizen of the programming language and you don't have to go through special components to work with it. LINQ is quite comprehensive and can be used to connect to database or non-database sources easily.
 
While LINQ depends on relatively complex concepts like Expression trees and Lambda expressions to work, their knowledge is not a requirement for you to use LINQ in your projects. The LINQ Query syntax is easy to learn and very intuitive. You can use the Query syntax to work with your data without worrying about what Lambda function it gets translated to.

  

 

Number of views (6584)/Comments (-)

Tags:
blog comments powered by Disqus

Enter your email below AND grab your spot in our big giveaway!

The winner will receive the entire Data Springs Collection 7.0 - Designed to get your website up and running like a DNN superhero (spandex not included).

Subscribe