Using LINQ, you can declaratively write the condition using an SQL-like statement, and the compiler does the job of retrieving the relevant data for you.
Suppose that you have an array of type string that contains a list of names. The following program prints out all the names in the string array that start with the character G:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQ {
class Program {
static void Main(string[] args) {
string[] allNames = new string[] {
'Jeffrey', 'Kirby', 'Gabriel',
'Philip', 'Ross', 'Adam',
'Alston', 'Warren', 'Garfield'
};
Console.ReadLine();
}
}
}
Using LINQ to Objects, you can rewrite the program as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQ {
class Program {
static void Main(string[] args) {
string[] allNames = new string[] {
'Jeffrey', 'Kirby', 'Gabriel',
'Philip', 'Ross', 'Adam',
'Alston', 'Warren', 'Garfield'
};
Console.ReadLine();
}
}
}
Notice that you have declared the foundNames
variable to be of type IEnumerable<string>
, and the expression looks similar to that of SQL:
IEnumerable<string> foundNames =
from name in allNames
where name.StartsWith('G')
select name;
The one important difference from SQL queries is that in a LINQ query the operator sequence is reversed. In SQL, you use the select-from-where
format, while LINQ queries use the format from- where-select
. This reversal in order allows IntelliSense to know which data source you are using so that it can provide useful suggestions for the where
and select
clauses.
The result of the query in this case is IEnumerable<string>
. You can also use the new implicit typing feature in C# 3.0 to let the C# compiler automatically infer the type for you, like this:
from name in allNames
where name.StartsWith('G')
select name;
When you now use a foreach
loop to go into the foundNames
variable, it will contain a collection of names that starts with the letter G. In this case, it returns Gabriel
, Garfield
.
The usefulness of LINQ is more evident when you have more complex filters. For example:
var foundNames =
from name in allNames
where name.StartsWith('G') && name.EndsWith('l')
select name;
In this case, only names that begin with 'G' and end with 'l' will be retrieved (Gabriel
).
Here's an example where you have an array of integer values. You want to retrieve all the odd numbers in the array and sort them in descending order (that is, the bigger numbers come before the smaller numbers). Using LINQ, your code looks like this:
int[] nums = {
12, 34, 10, 3, 45, 6, 90, 22, 87, 49, 13, 32
};
foreach (int n in oddNums)
Console.WriteLine(n);
And here's what the code will print out:
87
49
45
13
3
To find out the total number of odd numbers found by the query, you can use the Count()
method from the oddNums variable (of type IEnumerable<int>
) :
int count = oddNums.Count();
You can also convert the result into an int array, like this:
int[] oddNumsArray = oddNums.ToArray();