c# - Convert custom delimited string to List<myClass>? -


how 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 way using linq or :) in advance!

* note * should add know how splitting , looping through it, there has better way :)

this require separate tolist() call, query syntax declarative nature:

from s in x.split('|') let parts = s.split(':') select new myclass {      keyname = parts[0],      keyvalue = parts[1]  } 

or can use fluent syntax:

x.split('|')  .select(s => {     var parts = s.split(':');     return new myclass {          keyname = parts[0],          keyvalue = parts[1]      };  }).tolist() 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -