CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
A Detailed Look on LINQ Query

By Gaurav Arora
Posted On Mar 12,2009
Article Rating:
Be first to rate
this article.
No of Comments: 9
Category: C#/.NetFramework
Print this article.

A Detailed Look on LINQ Query

 

The article is related to previous posts,

1. Anonymous Types in LINQ

2. Testing Anonymous Type Equality in LINQ

 

Introduction

 

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)

  {

      Console.WriteLine(empbydept.Key);

      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.

 

 

Conclusion

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

 

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Hello ChristopherI'v
Hello ChristopherI've just read your comments on our wetbsie and the symptoms can be remedied by fitting the genuine Samsung RSH1 repair kit which is designed to fit the problem you have. I've spoken with our chief engineer regarding your comments on our wetbsie he confirms that you can fit the repair kit from inside without the need to pull the appliance out. It requires no previous technical assistance and you get our chief engineer telephone number for telephone support should you have any questions. http://awcbwvlhkpb.com [url=http://jefwtc.com]jefwtc[/url] [link=http://msfummsbd.com]msfummsbd[/link]
Hello MichaelI've ju
Hello MichaelI've just read your comments <a href="http://dtieauhei.com">prvoseuily</a> and the symptoms you have with your RS21 appliance can be remedied with the genuine Samsung RS21 repair kit which is designed to remedy it.Please verify that you definitely have a Coolzone model before ordering as the kits for the standard RS21 and the RS21 Coolzone are very different. If you have a J or K after the RS21 model number then you definitely have a Coolzone model.Assuming you definitely have a Coolzone here is the link to purchase the kit
we prefer to honor l
we prefer to honor lots of other world-wide-web web sites on the ietrennt, even though they aren?t linked to us, by linking to them. Below are some webpages worth checking out http://poypmd.com [url=http://epwdpqt.com]epwdpqt[/url] [link=http://fohbaq.com]fohbaq[/link]
Remarkable story, be
Remarkable story, believed we're able to blend a<a href="http://vdrwgglre.com"> neumbr</a> of unrelated data, nevertheless truly worth taking a look, wow did one know about Middle of the East has got a lot more problems just as well
Third Flower My spou
Third Flower My spouse and i have aelrady been now delighted that Albert could carry out his reports on account of the concepts he had through your online page. It really is now and again perplexing to only constantly be freely giving techniques which some people c
x7ICuc , [url=http://gudejuultkld.com/]gudejuultkld[/url], [link=http://luliyiofdvwp.com/]luliyiofdvwp[/link], http://bbnrbpdfyuxn.com/
x7ICuc , [url=http://gudejuultkld.com/]gudejuultkld[/url], [link=http://luliyiofdvwp.com/]luliyiofdvwp[/link], http://bbnrbpdfyuxn.com/
gyNxqg <a href="http://lyjlztzewquj.com/">lyjlztzewquj</a>
gyNxqg <a href="http://lyjlztzewquj.com/">lyjlztzewquj</a>
Y4jxJf <a href="http://jixngjgkdvjl.com/">jixngjgkdvjl</a>
Y4jxJf <a href="http://jixngjgkdvjl.com/">jixngjgkdvjl</a>
vLinq is really Great Tool. By using this tool eaisly create linq query.I came to know about this tool fromthe blog In this blog one more LINQ Query Builder Tool is mentioned that is LinqPad.But I use
vLinq is really Great Tool. By using this tool eaisly create linq query.I came to know about this tool fromthe blog In this blog one more LINQ Query Builder Tool is mentioned that is LinqPad.But I use vLinq. Good thing about this that It add-in the VS and query generate can be reused.Really Great Tool