An Introduction to LINQ - More Standard Query Operators
(Page 4 of 4 )
The From, Where and Select operators aren't the only ones provided by LINQ. Other useful operators are available, some of which we'll take a look at now. First, we'll look at the orderby operator which, as its name suggests, orders the results by a certain field. For example, say we want a list of everyone in our array with a phone number that starts with 555-292. Additionally, say we want this list to be sorted alphabetically by the person's first name. Sorting can be done simply by providing the orderby operator with a field by which to sort:
var number292 = from p in people
where p.Phone.StartsWith("555-292")
orderby p.Name
select p;
Dim number292 = From p In people _
Where p.Phone.StartsWith("555-292") _
Order By p.Name _
Select p
Be sure to take note of the space in the Visual Basic version of the operator.
We can also order the names in reverse alphabetical order (that is, in descending order) with the addition of a single word:
var reverse292 = from p in people
where p.Phone.StartsWith("555-292")
orderby p.Name descending
select p;
Dim reverse292 = From p In people _
Where p.Phone.StartsWith("555-292") _
Order By p.Name Descending _
Select p
Similar to the OrderBy operator is the GroupBy operator. Say we want to break our results into groups: those with 555-292 numbers, and those with 555-232 numbers (actually, this latter group contains only one person). Additionally, we want to alphabetize each group. To do all of this, we use both the OrderBy operator and the GroupBy operator. Let's query the array and then display the results by group. We'll start with C#:
var numberGrouped = from p in people
orderby p.Name
group p by p.Phone.Substring(0, 7) into g
select g;
foreach (var g in numberGrouped)
{
Console.WriteLine("{0} Numbers:", g.Key);
foreach (var p in g)
{
Console.WriteLine(p.Name);
}
}
The code is fairly straightforward. We specify what is to be grouped – p – and what it is to be grouped by – the first seven characters of the phone number. We also assign our group a variable name, g. Then, we loop through each group, printing the key (in this case, the first part of the phone number) and then the members of the group.
Now let's move on to Visual Basic:
Dim numberGrouped = From p In people _
Order By p.Name _
Group p By Phone = p.Phone.Substring(0, 7) Into Group _
Select New With {Phone, Group}
For Each g In numberGrouped
Console.WriteLine("{0} Numbers:", g.Phone)
For Each p In g.Group
Console.WriteLine(p.Name)
Next
Next
The code is about the same, but a few very important differences are present. First, we have to explicitly assign a variable name to the key. Second, we don't give the group a variable name. Though it is possible (g = Group), the resulting code is longer. Third, we don't just select the group. If we do, then we won't be provided with a Key property as in C#. Instead, we work with an anonymous type. So, as a result, the loop is different. The output, however, is identical to the output in the C# version.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |