convert comma separated string to list using linq

39,524

Solution 1

I don't neccesarily advocate this as a good solution but it does what you asked, which is to 'do it in LINQ'.

It is also probably not the most efficient solution.

string FirstName ="John1, John2, John3";
string MiddleInitial = "K1, K2, K3";
string LastName = "Kenndey1, Kenndey2, Kenndey3";
List<String> fn = new List<String>(FirstName.Split(','));
List<String> mi = new List<String>(MiddleInitial.Split(','));
List<String> ln = new List<String>(LastName.Split(','));

IEnumerable<NameDetails> result = from f in fn
        join i in mi on fn.IndexOf(f) equals mi.IndexOf(i)
        join l in ln on fn.IndexOf(f) equals ln.IndexOf(l)
        select new NameDetails {f, i, l};

I used LINQPad to try this out first (with an anonymous class, not the NameDetails calss).

Solution 2

You can use the Linq to CSV library.

Solution 3

Further to my previous answer you could also try:

// some setup
string firstName ="John1, John2, John3";
string middleInitial = "K1, K2, K3";
string lastName = "Kenndey1, Kenndey2, Kenndey3";
var fn = firstName.Split(',');
var mi = middleInitial.Split(',');
var ln = lastName.Split(',');

// the important bit
var s = fn.Select((f, index) => new NameDetails {f, i = mi[index], l = ln[index]});

It will be fragile to the string arrays having unequal numbers of entries.

Credit due to a previous answer on SO.

Share:
39,524
Prasad
Author by

Prasad

Updated on July 09, 2022

Comments

  • Prasad
    Prasad almost 2 years

    I have 3 comma separated strings FirstName, MiddleInitial, LastName

    I have a class NameDetails:

    public class NameDetails
    {
        public string FirstName { get; set; }
        public string MiddleInitial { get; set; }
        public string LastName { get; set; }
    }
    

    I have the values for strings as:

    FirstName ="John1, John2, John3"
    MiddleInitial = "K1, K2, K3"
    LastName = "Kenndey1, Kenndey2, Kenndey3"
    

    I need to fill the NameDetails List with the values from the comma separated strings.

    Any linq for this? I need this for my asp.net mvc (C#) application.

  • MichaelT
    MichaelT over 14 years
    You don't need List<string> Split method created array of string and arrays aleady can be accessed by LINQ
  • Graham Clark
    Graham Clark over 14 years
    Doesn't this show that Linq is probably not the best tool for this job? Seems like it would be far clearer to replace the Linq with a for loop.
  • rohancragg
    rohancragg over 14 years
    I agree, a for loop is perfectly adequate. I just fancied using this as an excuse to try out some LINQ.
  • rohancragg
    rohancragg over 14 years
    @MichaelT without the List<T> the IndexOf() is not available. I wasn't all that happy with using that anyway but it was the first thing I could think of that would work :-)
  • Prasad
    Prasad over 14 years
    thanks @rohancragg. Your both answers worked for me. But as per your comments, i will use this one.
  • Zain Shaikh
    Zain Shaikh over 13 years
    @rohancragg you could use Array.IndexOf() method in order to prevent using List<T>.