Splitting List<T> based on comma separated property value using Linq

Sergi Papaseit

Given this class:

public class Article
{
   public string Name { get; set; }
   public string Colour { get; set; }
}

Suppose I have the following List<Article> where some of the articles contain a comma separated name:

var list = new List<Article> 
{
    new Article { Name = "Article1, Article2, Article3", Colour = "Red" },
    new Article { Name = "Article4, Article5, Article6", Colour = "Blue" },
}

Is there a way, in one Linq statement, to get a list where each comma separated name becomes a separate article?

var list = new List<Article> 
{
    new Article { Name = "Article1", Colour = "Red" },
    new Article { Name = "Article2", Colour = "Red" },
    new Article { Name = "Article3", Colour = "Red" },
    new Article { Name = "Article4", Colour = "Blue" },
    new Article { Name = "Article5", Colour = "Blue" },
    new Article { Name = "Article6", Colour = "Blue" },
}
Sergey Kalinichenko

You can do it with SelectMany:

var res = list.SelectMany(
    art => art.Name.Split(',').Select(n =>
        new Article {
            Name = n.Trim()
        ,   Colour = art.Colour
        }
    )
);

Demo.

I was hoping there was a way that would save me newing-up an object since my class [...] has quite a bit more than two properties

Although you cannot avoid new-ing altogether, you can improve readability of your code quite a bit by hiding the copying of individual properties inside the Article, like this:

public class Article {
    public string Name { get; set; }
    public string Colour { get; set; }
    public Article Rename(string newName) {
        return new Article {
            Name = newName
        //  Copy the remaining attributes
        ,   Colour = this.Colour
        };
    }
}

Now your LINQ would look like this:

var res = list.SelectMany(art => art.Name.Split(',').Select(n => art.Rename(n.Trim())));

This did not remove any code, simply reshuffled some of it. However, placing the copying of Article's properties into Article reduces the chance that you wold forget to copy newly added properties.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Splitting a column value into list of values separated by comma

From Dev

Using Linq to find a value in comma separated value in a List

From Dev

MySQL Splitting string value separated by comma using INSERT INTO/SELECT

From Dev

Usinq Linq to get rows based on comma-separated value

From Dev

splitting comma separated list into a temp table

From Dev

Splitting a comma separated value in a QueryString into 2 results

From Dev

How to create comma separated list of numbers based on numeric value in column

From Dev

Split string separated by comma but without splitting double value

From Dev

Comma separated using string.Join with LINQ

From Java

Using Linq to return a Comma separated string

From Dev

Defining comma separated strings in EF and using with Linq

From Dev

Comma separated using string.Join with LINQ

From Dev

Check if value exists in a comma separated list

From Dev

Regular Expression for last value in comma separated list

From Dev

Turn a value range in a cell into a comma separated list

From Dev

Find value in comma-separated list

From Dev

Find string value in comma-separated list

From Dev

Turn a value range in a cell into a comma separated list

From Dev

Splitting comma separated values in Oracle

From Dev

Splitting a comma separated string MySql

From Dev

MySQL: creating comma separated list and using in IN

From Dev

Produce a comma separated list using StringBuilder

From Dev

using decode with input as comma separated list

From Dev

Extract value based on column header from Comma separated file using bash

From Dev

Splitting a list of objects based on a property c#

From Dev

LINQ filter list based on property value and filter the properties as well

From Dev

How to determine if all objects inside List<T> has the same property value using Linq

From Dev

Linq, combining multiple records into comma separated string, grouped by distinct value

From Dev

Easiest way to convert list to a comma separated string by a Specific Property?

Related Related

  1. 1

    Splitting a column value into list of values separated by comma

  2. 2

    Using Linq to find a value in comma separated value in a List

  3. 3

    MySQL Splitting string value separated by comma using INSERT INTO/SELECT

  4. 4

    Usinq Linq to get rows based on comma-separated value

  5. 5

    splitting comma separated list into a temp table

  6. 6

    Splitting a comma separated value in a QueryString into 2 results

  7. 7

    How to create comma separated list of numbers based on numeric value in column

  8. 8

    Split string separated by comma but without splitting double value

  9. 9

    Comma separated using string.Join with LINQ

  10. 10

    Using Linq to return a Comma separated string

  11. 11

    Defining comma separated strings in EF and using with Linq

  12. 12

    Comma separated using string.Join with LINQ

  13. 13

    Check if value exists in a comma separated list

  14. 14

    Regular Expression for last value in comma separated list

  15. 15

    Turn a value range in a cell into a comma separated list

  16. 16

    Find value in comma-separated list

  17. 17

    Find string value in comma-separated list

  18. 18

    Turn a value range in a cell into a comma separated list

  19. 19

    Splitting comma separated values in Oracle

  20. 20

    Splitting a comma separated string MySql

  21. 21

    MySQL: creating comma separated list and using in IN

  22. 22

    Produce a comma separated list using StringBuilder

  23. 23

    using decode with input as comma separated list

  24. 24

    Extract value based on column header from Comma separated file using bash

  25. 25

    Splitting a list of objects based on a property c#

  26. 26

    LINQ filter list based on property value and filter the properties as well

  27. 27

    How to determine if all objects inside List<T> has the same property value using Linq

  28. 28

    Linq, combining multiple records into comma separated string, grouped by distinct value

  29. 29

    Easiest way to convert list to a comma separated string by a Specific Property?

HotTag

Archive