programming4us
programming4us
ENTERPRISE

LINQ to Objects : How to Group Elements (part 5) - Projecting Grouped Elements into a New Type

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

5. Projecting Grouped Elements into a New Type

The general group by function when using query expression syntax returns the same element type as the source collection elements. If you would like the grouped elements to be in a different form, you must use the extension method syntax and specify your own “element selector” expression. The element selector expression takes an instance of the original element and returns a new type based on the supplied expression.

Listing 7 demonstrates how to group Contact records by state and return a group of strings rather than a group of Contact instances for each element (Contact is the original element type). In this case, a string is returned by concatenating the contact’s first and last name. The Console output result is shown in Output 4.

Listing 7. Group elements can be projected to any type. This sample projects group elements as a string type, rather than the default Contact instance—see Output 4
IList<Contact> contacts = Contact.SampleData();

var q = contacts.GroupBy(
c => c.State,
c => c.FirstName + " " + c.LastName);

foreach (var group in q)
{
Console.WriteLine("State: {0}", group.Key);
foreach (string name in group)
Console.WriteLine(" {0}", name);
}

Output 4.
State: CA
Barney Gottshall
Mandy Gottshall
Anthony Gauwain
Jeffery Deane
State: WA
Bernadette Gottshall
Armando Valdes
Stewart Kagel
Chance Lard
State: AK
Adam Gauwain
Chris Gauwain
State: FL
Collin Zeeman
State: TX
Blaine Reifsteck
Mack Kamph
State: OR
Ariel Hazelgrove

To demonstrate a slightly more complicated example, Listing 8 demonstrates projecting each group element into an anonymous type. This example also shows how to use the Null-Coalescing Operator to handle missing data in either the grouping predicate or the element projection expression. The Console output is shown in Output 5.

Listing 8. Grouping elements and projecting into an anonymous type and how to handle null values using the null-coalescing operator—see Output 5
List<Contact> contacts = Contact.SampleData();

var q = contacts.GroupBy(
c => c.State ?? "state unknown",
c => new
{
Title = c.FirstName + " " + c.LastName,
Email = c.Email ?? "email address unknown"
});

foreach (var group in q)
{
Console.WriteLine("State: {0}", group.Key);
foreach (var element in group)
Console.WriteLine(" {0} ({1})",
element.Title,
element.Email);
}

Output 5.
State: CA
Barney Gottshall ([email protected])
Mandy Gottshall (email address unknown)
Anthony Gauwain (email address unknown)
Jeffery Deane ([email protected])
State: WA
Bernadette Gottshall (email address unknown)
Armando Valdes ([email protected])
Stewart Kagel ([email protected])
Chance Lard ([email protected])
State: AK
Adam Gauwain ([email protected])
Chris Gauwain (email address unknown)
State: FL
Collin Zeeman ([email protected])
State: TX
Blaine Reifsteck ([email protected])
Mack Kamph ([email protected])
State: OR
Ariel Hazelgrove ([email protected])

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