ENTERPRISE

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

12/30/2013 8:46:59 PM

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 (bgottshall@aspiring-technology.com)
Mandy Gottshall (email address unknown)
Anthony Gauwain (email address unknown)
Jeffery Deane (jeff.deane@aspiring-technology.com)
State: WA
Bernadette Gottshall (email address unknown)
Armando Valdes (val1@aspiring-technology.com)
Stewart Kagel (kagels@aspiring-technology.com)
Chance Lard (lard@aspiring-technology.com)
State: AK
Adam Gauwain (adamg@aspiring-technology.com)
Chris Gauwain (email address unknown)
State: FL
Collin Zeeman (czeeman@aspiring-technology.com)
State: TX
Blaine Reifsteck (blaine@aspiring-technology.com)
Mack Kamph (mack.kamph@aspiring-technology.com)
State: OR
Ariel Hazelgrove (arielh@aspiring-technology.com)

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
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8