LINQ In C#.Net-trickcode

LINQ In C#.Net,LINQ,SQL command, LINQ to SQL,LINQ to XML,Different Flavors of LINQ,Language-Integrated Query (LINQ)
Share it:
LINQ In C#.Net
google

Language-Integrated Query (LINQ)


LINQ Stand for Language-Integrated Query, It was introduced in .NET version 3.5 to allow a programmer to query data from many kinds of data sources without knowing any external language. Querying is the process of obtaining data from a data source. LINQ makes it very easy for you to query data from different kinds of data sources. LINQ is integrated into C# and VB, and multiple special keywords and syntax for querying using LINQ have been added.


The arrival of LINQ, programmers write a different set of codes for querying different data sources. For example, they have to write codes for querying an SQL database using an SQL command or using XPath for querying XML files. LINQ querying different data sources requires only the knowledge of the LINQ keywords and methods that were added in .NET 3.5.

Different Flavors of LINQ

Visual Studio already includes some of these providers such as LINQ to Objects. This section of the site will focus on LINQ to Objects which is used to query a collection of objects in your code that implements the IEnumerable<T> interface. There is also a LINQ to SQL which is specifically designed to make it easier to query SQL Server databases. For querying XML files, We can use the LINQ to XML. You can extend LINQ to query more kinds of data sources. we can create your own providers if you want to support querying another type of data source using LINQ. The querying techniques that will be thought in the following lessons can be applied to the different flavors of LINQ.


LINQ is made possible by the extension methods that are attached to the IEnumerable<T> interface. You can call these methods directly but you need to have a knowledge lambda expressions. we can also use query expressions in which syntax looks like SQL. The Query expressions are the main tool you will use to query data using LINQ although you can call the extension methods and use lambda expressions.



C#.net is an imperative language which means that you write the step by step codes to make something happen, but LINQ promotes declarative programming. For example, suppose that we want to get all the even numbers from an array. Without LINQ and using the imperative style of programming, your code will look like this:

List evenNumbers = new List();
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

foreach(int num in numbers)
{
   if (num % 2 == 0)
      evenNumbers.Add(num);
}

You instruct the computer to go through every value in a collection and then retrieve the number that matches the condition in an if statement. Now take a look at the declarative version uses the query expression syntax.
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = from n in numbers
                  where n % 2 == 0
                  select n;
}


Share this article with your friends
Share it:

C#

LINQ In C#.Net

Post A Comment:

0 comments: