Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Populate a Dictionary
Message
 
To
23/03/2011 11:55:11
General information
Forum:
ASP.NET
Category:
LINQ
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01504070
Message ID:
01504779
Views:
42
>You are trying to put too much into just a list. Your list can only have a single string value for each entry. You will need either a list of key value pairs, or a dictionary. Try one of the following:
>
DataTable dt = ds.Tables[0];
>List< KeyValuePair<string, string>> MyList = dt.Columns.OfType<DataColumn>().Select(dc => new KeyValuePair<string, string>(dc.ColumnName, dt.Rows[0][dc].ToString())).ToList();
>
>or
>
DataTable dt = ds.Tables[0];
>Dictionary<string, string> MyDictionary = dt.Columns.OfType<DataColumn>().ToDictionary(dc => dc.ColumnName, dc => dt.Rows[0][dc].ToString());
>
Sorry Rob - I meant Dictionary - bit of thread drift creeping in - I've been working with lists and dictionaries in the same thread - my bad - your second suggestion looks perfect - where did you learn all this stuff ?
Regards,
Peter J. Kane



Pete
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform