Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list in C# defined like this:

public class Author  
{  
    public string Name { get; set; }  
    public string Book { get; set; }
    public string Age { get; set; }
    public string Address { get; set; }
    
} 

List<Author> originalAuthorList = new List<Author>
{
    new Author{ Name = "Bob", Age = "30", Address = "123 Main St.", Book = "Bob's First Book", },
    new Author{ Name = "Dan", Age = "32", Address = "456 main St.", Book = "Dan's First Book" },
    new Author{ Name = "Dan", Age = "32", Address = "456 main St.", Book = "Dan's Second Book" },
    new Author{ Name = "Tom", Age = "34", Address = "789 main St.", Book = "Tom's First Book" }
};


How could I rearrange the list to look like this:
List<Author> newAuthorList = new List<Author>
{
    new Author{ Name = "Bob", Age = "30",  Address = "123 Main St.", Book = "Bob's First Book" },
    new Author{ Name = "Dan", Age = "32",  Address = "456 Main St.", Book = "Dan's First Book, Dan's Second Book" },
    new Author{ Name = "Tom", Age = "34",  Address = "789 Main St.", Book = "Tom's First Book" }
};


What I have tried:

I know that if I only had 2 fields, for example, Name and Book, I could do this:

List<Author> newAuthorList = originalAuthorList
    .GroupBy(
        a => a.Name, // Group by name
        a => a.Book) // Select the book as the element
    .Select(g => new Author
    {
        Name = g.Key,
        Book = string.Join(", ", g) // Join the books together
    })
    .ToList();


But, how would I include all 4 fields in the new list?
Posted
Updated 23-Sep-20 22:09pm
Comments
BillWoodruff 23-Sep-20 16:48pm    
if you have control over the structure of the Author Class, and the data format used to create instances of that Class: if you can change 'Books from string to List<string> :

I'll share some code with you ... on your request

If you are familiar with Linq, you can do something like this


C#
var authors = originalAuthorList.
GroupBy(a => (a.Name, a.Age, a.Address), a => a.Book);
foreach (var grp in authors)
  {
   Console.WriteLine($"Name={grp.Key.Name}, Age={grp.Key.Age}, Address={grp.Key.Address} Books {string.Join(", ", grp)}");
  }
 
Share this answer
 
Comments
BillWoodruff 24-Sep-20 15:35pm    
+5 I learned something from your use of String.Join in the code ! That is an advanced example.

Might as well convert to Dictionary ?

var dict = authors.ToDictionary(
grp => grp.Key, grp => string.Join(", ", grp)
);

thanks, Bill
George Swan 24-Sep-20 16:56pm    
Thanks Bill. Using a Dictionary is probably the most efficient solution. I find that there are usually faster solutions than Linq provides but they are not as much fun to write. Regards, George.
Start here: Using Linq to create a Dictionary of sub-Lists by grouping from a collection.[^]
That lets you create a Dictionary which has the author name as the Key, and a List of all his Author entries as a Value.
You can then run through each group and use String.Join[^] to change the books to a comma list under a new Author entry.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900