2. Specifying a Key to Group By
During evaluation, all elements that share common key values as evaluated by the keySelector expression will be grouped. Value equality is determined using the default Equals operator, although this behavior can be overridden by specifying a custom equality comparison function, as seen shortly.
The keySelector
expression can be any expression that evaluates a consistent return
result for the desired grouping scheme. It is often a single field
value from the source collection, but it can be as complex as needed. Listing 1 demonstrates how to group using the first three characters of a set of strings (the final groupings are shown in Output 1).
Listing 1. Elements that share the same return key value from the key selector function will be grouped—see Output 1
string[] partNumbers = new string[] { "SCW10", "SCW1", "SCW2", "SCW11", "NUT10", "NUT1", "NUT2", "NUT11" };
var q = from pn in partNumbers group pn by pn.Substring(0,3);
foreach (var group in q) { Console.WriteLine("Group key: {0}", group.Key); foreach (var part in group) { Console.WriteLine(" - {0}", part); } }
|
Output 1.
Group key: SCW - SCW10 - SCW1 - SCW2 - SCW11 Group key: NUT - NUT10 - NUT1 - NUT2 - NUT11
|
Handling Null Values in keySelector Expressions
Some care when writing the key selector is required for handling null values in the source data. The code shown in Listing 1 will fail if any of the part-number strings are null in value the moment the Substring
method is attempted. Null values are safely handled in general, but if
the range variable is processed within the key selector function, the
normal null handling rules apply. Thus it is necessary to confirm an
element isn’t null before attempting to access properties or call
methods on these range variables.
The following code throws a NullReferenceException when the query q is first accessed for iteration:
string[] partNumbers = new string[] { "SCW10", null,
"SCW2", "SCW11", null, "NUT1", "NUT2", null };
// beware - nulls in source data can break your group
// expression. This grouping will fail.
var q = from pn in partNumbers
group pn by pn.Substring(0, 3);
To protect against this circumstance, check the key
selection expression for null values and throw all those elements into
a group of your choosing to keep the exception from being thrown and to
allow the null grouping to be orderly processed, as the following code
demonstrates:
string[] partNumbers = new string[] { "SCW10", null,
"SCW2", "SCW11", null, "NUT1", "NUT2", null };
// Advice - Guard against null values in key selection
var q = from pn in partNumbers
group pn by
pn == null ? "(null)" : pn.Substring(0,3);
The ternary operator (?) and the null-coalescing operator (??) offer a convenient syntax for handling potential null values in the keySelector expression. Listing 2 demonstrates their use in protecting against a null property value. In this case both operators handle the State property being null and groups these elements under the key value string of (null).
The ternary operator shortens the common if (condition) then (true expression) else (false expression) pattern of code to (condition) ? (true expression) : (false expression).
This is especially useful when handling null values within query
expressions where a full if-then-else statement is not valid syntax.
The null-coalescing operator is used to define a default value if the variable it follows has a null value, that is, X = (variable) ?? (default if null). Listing 2 demonstrates this usage.
|
Listing 4-2. Example of using the null-coalescing operator and the ternary operator to protect keySelector expressions from null values
// Guard against null data using
// ternary (? as shown)
var q1 = from c in Contact.SampleData()
group c by
c.State == null ? "(null)" : c.State;
// Guard against null data using
// the null coalescing operator (??).
var q2 = from c in Contact.SampleData()
group c by
c.State ?? "(null)";