0

I have a List like,

Class Traking {
public sting TrackID {get;set}
public sting AlertID {get;set} // Comma separated Values
}

            TrackID    AlertID
            1001       101,102,103
            1002       201,202,203
            1003       301,302,303

AlertID having comma seperated values, Using Linq convet to Like this

           TrackID   AlertID
           1001       101
           1001       102
           1001       103
           1002       201
           1002       202
           1002       203
           1003       301
           1003       302
           1003       303

Ex:
Turning a Comma Separated string into individual rows - This done from SQL, the same way need to do in LinQ

I hope understand my problem

Rajkumar
  • 111
  • 9

1 Answers1

1

If your object looks like this:

class Traking 
{
    public string TrackID {get;set;}
    public string AlertID {get;set;} // Comma separated Values
}

And your list looks like this:

var ls=new List<Traking>
    {
        new Traking(){TrackID = "1001",AlertID = "101,102,103"},
        new Traking(){TrackID = "1002",AlertID = "201,202,203"},
        new Traking(){TrackID = "1003",AlertID = "301,302,303"},
    };

Then you can do this:

var result= ls.SelectMany (l => l.AlertID.Split(',').Select (s =>new Traking()
    {
        TrackID=l.TrackID,
        AlertID=s
    } )).ToList()

To get this output:

TrackID    AlertID
1001       101 
1001       102 
1001       103 
1002       201 
1002       202 
1002       203 
1003       301 
1003       302 
1003       303 
Arion
  • 31,011
  • 10
  • 70
  • 88