programming4us
programming4us
ENTERPRISE

LINQ to Objects : How to Group Elements (part 2) - Specifying a Key to Group By

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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).

Using the Ternary (?) and Null Coalescing (??) Operators

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)";
Other  
  •  Moving into SAP Functional Development : Gaining Control of Change Control - Change Management Best Practices and Approaches (part 4)
  •  Moving into SAP Functional Development : Gaining Control of Change Control - Change Management Best Practices and Approaches (part 3)
  •  Moving into SAP Functional Development : Gaining Control of Change Control - Change Management Best Practices and Approaches (part 2)
  •  Moving into SAP Functional Development : Gaining Control of Change Control - Change Management Best Practices and Approaches (part 1)
  •  Moving into SAP Functional Development : Gaining Control of Change Control - An Overview of Change Management
  •  Performing mySAP.com Component Installations : Addressing General mySAP Post-Installation Tasks
  •  Programming .NET Components : Working with Threads (part 5) - Thread States, Thread Priority and Scheduling
  •  Programming .NET Components : Working with Threads (part 4) - Aborting a Thread
  •  Programming .NET Components : Working with Threads (part 3) - Blocking Threads
  •  Programming .NET Components : Working with Threads (part 2) - Creating Threads
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us