[RESOLVED] Convert Dictionary to DataTable
Hello Everybody,
How can I convert following categories object into DataTable, or alternatively how can I use the same to loop through individual values?
Code:
Dictionary<Guid, UHCategory> categories = new Dictionary<Guid, UHCategory>();
categories = ClassProvider.GetCategoryCollection();
Thanks.
Re: Convert Dictionary to DataTable
First up, don't create objects that you never use. Why create a new Dictionary, only to throw it away the very next line with one you get from somewhere else? If GetCategoryCollection is going to give you a Dictionary object, why are you creating one on the previous line that you never use?
As for the question, you would have to build the DataTable schema yourself, then loop through the items in the Dictionary and add a DataRow for each one. I don't know what a UHCategory is but, as an example:
csharp Code:
DataTable table = new DataTable();
table.Columns.Add("CategoryID", typeof(Guid));
table.Columns.Add("CategoryName", typeof(string));
foreach (KeyValuePair<Guid, UHCategory> category in categories)
{
table.Rows.Add(category.Key, category.Value.Name);
}