Convert DataRowCollection to NameValueCollection
I need an elegant way to convert a DataRowCollection to a N开发者_JS百科ameValueCollection.
note: during the conversion I need a section where I can invoke an inflector to tidy up key names
You need to loop over the rows and add the desired columns manually.
var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "Hello");
dt.Rows.Add(2, "World");
dt.Rows.Add(1, "Foo"); // dupe key
var nvc = new NameValueCollection();
foreach (DataRow row in dt.Rows)
{
string key = row[0].ToString();
// tidy up key here
nvc.Add(key, row[1].ToString());
}
// display nvc
foreach (var key in nvc.AllKeys)
Console.WriteLine("{0}: {1}", key, nvc[key]);
If you're interested in a Dictionary
you could use ToDictionary
, however this won't work if duplicate keys are allowed. In that case another elegant solution would be to use ToLookup
.
// optionally use a Func to do the tidying up
// or call an existing method or do it inline for simple operations
Func<string, string> tidyFunc = (s) => s.Trim();
var lookup = dt.Rows.Cast<DataRow>()
.ToLookup(r => tidyFunc(r[0].ToString()), r => r[1].ToString());
foreach (var g in lookup)
{
Console.WriteLine("Key: " + g.Key);
foreach (var item in g)
Console.WriteLine(item);
}
精彩评论