.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

LINQ Query - Descriptive in a tweak

Posted By:Gaurav Arora       Posted Date: April 28, 2009    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  

A Detailed Look on LINQ Query




LINQ - Language Integrated Query is a term that helps language developers to use its flexibility to query collections of object using SQL syntax. Moving forward, this article will elaborate the idea "How to describe a LINQ Query and its Basics".


In simple terms, a query is an expression that retrieves data from a data source.

Scope of this Article

1.      Creating a LINQ project using Visual Studio

2.      Basics

3.      LINQ Actions and some basic examples


Creating a LINQ Project


As per MSDN,

To use LINQ to XML or LINQ to DataSet in either C# or VB.Net language, you must manually add namespaces and references as described in the following:


If you are upgrading a project that you created by using an earlier version of Visual Studio, you may have to supply these or other LINQ -related references manually and also manually set the project to target .NET Framework version 3.5.


To target the .NET Framework version 3.5 in Visual Studio 2008


In Visual Studio 2008, open a Visual Basic or C# project that was created in Visual Studio 2005 and follow the prompts to convert it to a Visual Studio 2008 project.


1.      For a C# project, click the Project menu, and then click Properties.

In the Application property page, select .NET Framework 3.5 in the Target Framework drop-down list.


2.      For a Visual Basic project, click the Project menu, and then click Properties. In the Compile property page, click Advanced Compile Options and then select .NET Framework 3.5 in the Target Framework (all configurations) drop-down list.



Basic Operations of LINQ Query


LINQ query consists three distinct actions as defined bellow:


 (1) In this step, LINQ gather the DataSource, it may be any source of data.

 (2) In next step, have to create the Query.

 (3) Finally, execute the Query.


Lest, go through the following code-lines and try to understand the above operations:


class LINQQuery


    static void Main()



        //  This is the Data source - Operation - I

        int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };


        //  Query Creation - Operation - II

        var numQuery =

            from num in numbers

            where (num % 2) == 0

            select num;


        //  Query Execution - Operation - III

        foreach (int num in numQuery)


            Console.Write("{0,1} ", num);






Three actions of LINQ Query [adapted from MSDN - bb397906]


Important Points

1.      LINQ queries start with the keyword from.

2.      In a LINQ query, you are always working with objects.


You can apply the basic operations to LINQ Query as follow:


1.      Filtering is the common operation through which you can filter the data, consider following code:

//filter all employees on last name as 'Arora'

var lnqueryNames = from empname in employee

where empname.lastname == "Arora"

select empname;


2.      Ordering  sets the ordering for the output, lets take a look in the following:

//display all Arora in ascending order with departments

var lnqueryNames = from empname in employee

                           where empname.lastname == "Arora"

                           orderby empname.dept ascending

                           select empname;


3.      Ordering will give the output in a group, consider following example:


var lnqueryNames = from empname in employee

                           group empname by empname.department;


foreach (var empbydept in lnqueryNames)



      foreach (Employee emp in empbydept)


          Console.WriteLine(" Last Name:{0}", emp.lastName);




Skip and Take utilities of LINQ


Skip, SkipWhile, Take, and TakeWhile are used to partition collections into two parts and then return one of the parts. These partition features are only implemented as extension methods.


Ø       Skip jumps to the indicated argument position in the sequence and returns everything after that.


Ø       SkipWhile accepts a predicate and instead of using a position, it skips until the predicate evaluates to false.


Ø       Take grabs all of the elements up to the specified position in the index.


Ø       TakeWhile grabs all of the items in a sequence as long as the predicate evaluates to true.



Thus, we have learnt basics of LINQ queries and constructing LINQ queries with some basic examples. Stay tuned for more LINQ articles!


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend