1

How do I turn this:

string x = "key:value|key:value|key:value|key:value";

into this?

List<myClass> listObj;

myClass definition:

public class myClass
{
    public string keyName { get; set; }
    public string keyValue { get; set; }
}

There has to be a way to do it using LINQ or something :) thanks in advance!

* NOTE * I should add I know how to do this splitting it and looping through it, but there has to be a better way :)

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Losbear
  • 3,255
  • 1
  • 32
  • 28
  • possible duplicate of [Convert array of strings to List](http://stackoverflow.com/questions/10129419/convert-array-of-strings-to-liststring) – egrunin Jul 18 '13 at 15:01
  • 3
    You can use Dictionary, and String.Split() method.. – YD1m Jul 18 '13 at 15:01
  • Not a duplicate - the other article is a single string array. My question is about having a list within a list (multidimensional). – Losbear Jul 18 '13 at 15:19
  • @egrunin, it's not a duplicate of that question, as the OP really has a string (with a faked array of keyvaluepair's inside it). not an array of strings. Perhaps we need to change the title, though I'm not sure how to refer to that. – Tim Jul 18 '13 at 15:19
  • Retracted close vote. – egrunin Jul 18 '13 at 21:28

3 Answers3

10

This will require separate ToList() call, but I like query syntax for its declarative nature:

from s in x.Split('|')
let parts = s.Split(':')
select new myClass { 
    keyName = parts[0], 
    keyValue = parts[1] 
}

Or you can use fluent syntax:

x.Split('|')
 .Select(s => {
    var parts = s.Split(':');
    return new myClass { 
        keyName = parts[0], 
        keyValue = parts[1] 
    };
 }).ToList()
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

Do this if you prefer to use your custom class instead of Dictionary

 var result = from y in x.Split('|')
             let obj = y.Split(':')
             select new myClass{keyName = obj[0], keyValue = obj[1]};

 var list = result.ToList();   
Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

Well, since you really wanted to avoid splitting and looping...

public List<MyClass> Parse(string base, string workingName, string workingValue,
                           bool processingName = true, 
                           List<MyClass> workingList = null, int index = 0)
{
    if (workingList == null)
        workingList = new List<MyClass>();

    if (index >= base.Length)
    {
        return workingList;
    }
    if (base[index] = '|')
    {
        workingList.Add(new MyClass { keyName = workingName, keyValue = workingValue });
        return Parse(base, "", "", true, workingList, index + 1);
    }
    else if (base[index] = ':')
    {
        return Parse(base, workingName, "", false, workingList, index + 1);
    }
    else if (processingName)
    {
        return Parse(base, workingName + base[index], "", processingName, workingList, index + 1);
    }
    else
    {
        return Parse(base, workingName, workingValue + base[index], processingName, workingList, index + 1);
    }
}

But please, for the love of whatever you hold dear, don't do anything even remotely resembling that (and yes, this is untested, hand-written code, so there are probably errors - just making a joke about avoiding things).

Tim
  • 14,999
  • 1
  • 45
  • 68