what is linq in c sharp


LINQ stands for Language Integrated Query, a small language for querying data sources. For all practical purposes, it’s an extension to Visual Basic. More specifically, LINQ consists of statements that you can embed into a program to select items from a collection based on various criteria. Unlike a loop that examines each object’s properties and either selects or rejects the object, it, LINQ is a declarative language: It allows you to specify the criteria, instead of specifying how to select the objects. A declarative language, as opposed to a procedural language, specifies the operation you want to perform, and not the steps to take. VB is a procedural language; the language of SQL Server, T-SQL, is a declarative language.

what is linq in c sharp

What Is LINQ?
Although defining LINQ is tricky, a simple example will demonstrate the structure of LINQ and what it can do for your application. Let’s consider an array of integers: Dim data() As Int16 = {23, 10, 5, 4, 6, 4, 12, 4, 45, 42, 65}
To select specific elements of this array, you’d write a For…Next loop, examine each element of the array, and either select it by storing it into a new array or ignore it. To select the elements that are numerically smaller than 10, you’d write a loop like the following:
Dim smallNumbers As New ArrayList
Dim itm As Integer
For Each itm In data
If itm < 10 Then
End If
Can you see why I’m using an ArrayList to store the selected values? I could have
used another array, but arrays must be declared with dimensions, and I’d have to resize
the array or start with an array large enough to store all the elements. Let’s do the same
with LINQ:
Dim smallNumbers = From n In data
Where n < 10
Select n
This is a peculiar statement indeed, unless you’re familiar with SQL, in which case you can
easily spot the similarities. LINQ, however, is not based on SQL, and not every operation has
an equivalent in both. Both SQL and LINQ, however, are declarative languages that have many
similarities. If you’re familiar with SQL, you have already spotted the similarities and the fact
that LINQ rearranges the basic elements. Whereas SQL can be used only with database tables,
LINQ can be applied to collections, XML files, and SQL tables. It’s a uniform language for
querying data from several data sources.
Let’s look at the LINQ query in greater detail:
Dim smallNumbers = From n In data
Where n <10
select n
Start with the structure where the selected elements will be stored; this structure will hold the result of the query. The smallNumbers variable is declared without a type, because its type is determined by the type of the collection you’re querying. Type inference, which must be turned on for this statement to work, makes this possible. If type inference is off, you must declare the variable that will accept the result of the query.
Because the query selects elements from the data array, the smallNumbers variable can’t represent anything but an array of integers. Actually, it’s not exactly an array of integers; it’s a typed collection of integers that implements the IEnumerable interface. If you hover the pointer over the name of the collection, you’ll see that its type is IEnumerable(Of Short).
The LINQ query starts after the equals sign with the From keyword, which is followed by a variable that represents the current item in the collection, followed by the In keyword and the name of the collection. The variable is called the control variable, just like the variable of a For…Next or For Each loop, and it represents the entity you’re working with. If the collection is an array of integers, the control variable is an integer; if the collection is a List of Customer objects, then the control variable is a Customer object. The first part of the query specifies the collection we’re going to query. Then comes the Where clause that limits the selection. The Where keyword is followed by an expression that involves the control variable; this expression limits your selection. In this extremely trivial example, I select the elements that are less than 10. The expression of the Where clause can become quite complicated, as you will see shortly. It’s a VB-like expression that’s evaluated for every element in the collection; it returns a True/False value; and it determines which elements will be selected, and which elements will be ignored.
The last keyword in the expression, the Select keyword, determines what you’re selecting. In most cases, you will select the control variable itself but not always. Here’s a variation of the previous query expression:
Dim evenValues = From n In data _
Where m mod 2 = 0
Select “Number ” & n.ToString & ” is even”
Here, I select the even numbers from the original array and then form a string for each of the selected values. This time, the result of the query (the evenValues variable) is an IEnumerable of strings, as determined by the compiler. The Select clause is optional; if you omit it, the query will return the control variable itself.
But why bother with a new component to select values from an array? A For Each loop that processes each item in the collection is not really complicated and is quite efficient. LINQ is a universal approach to querying data, and it can be used to query all kinds of data, from XML documents to databases. When you work with databases, for example, you can’t afford to move all the data to the client, select a few of them from within a loop, and ignore the rest. A database table may contain thousands and thousands of rows, and you may end up selecting a few hundred of them. LINQ is smart enough to execute the appropriate statements against the database and return to the client only the data you’re interested in. I’ll discuss these aspects of LINQ and databases (LINQ to SQL) in much detail later in this chapter.
As I have mentioned in the introduction, LINQ isn’t limited to arrays and collections.
Although iterating through an array of integers, or even objects, may be trivial, the same isn’t true for XML documents. LINQ allows you to select the elements of an XML document you’re interested in without having to navigate through the nodes of the document, as we did in the previous chapter. At any rate, LINQ is the latest and hottest technology from Redmond — it may very well become a universal language, just like SQL. It’s a very powerful and flexible tool that can be used in many diverse situations. It’s certainly much more flexible than straight VB code, because it’s a declarative language. Remember that with LINQ, you specify what you want to do, not how the computer will do it.

Leave A Reply

Your email address will not be published.