Andy Green
asked on
Convert object data to CSV for display.
Hi
I have an object to work with that has a data field of 'resources' that are presented as a set of name / value pairs. The attached image shows what these look like.
How would I get this data into a comma separated list for display?
Regards
Andy
Locs.png
I have an object to work with that has a data field of 'resources' that are presented as a set of name / value pairs. The attached image shows what these look like.
How would I get this data into a comma separated list for display?
Regards
Andy
Locs.png
ASKER
Hi
Its not the whole object I want to parse out rather just the resource data item.
My initial attempt was to cast the resource dataitem as an IDictonary and use LINQ to parse that out, then wondered whether there was a more efficient way,
Andy
Its not the whole object I want to parse out rather just the resource data item.
My initial attempt was to cast the resource dataitem as an IDictonary and use LINQ to parse that out, then wondered whether there was a more efficient way,
Andy
It would be better if you give more information about the type of "resources" and how you access it. And more about requirements. However if it can be casted into IDictionary or Dictionary the way you mentioned above seems to be a right way I believe.
May be use something like
May be use something like
String names = string.Join(", ", myDict.Select(x => x.Key + "-" + x.Value));
orstring s = string.Join(";", myDict.Select(x => x.Key + "=" + x.Value).ToArray());
or simplymyDict.Select(x => x.Key + "=" + x.Value).ToArray();
ASKER
Thanks, How do I get this into a Dictionary. I have tried several ways and this one runs OK but always returns null.
var resourceList = DataBinder.Eval(e.Item.Dat aItem, "Resources") as System.Collections.Generic .Dictionar y<string, string>;
The shape of the data can be seen in the image in the OP/
Regards
Andy
var resourceList = DataBinder.Eval(e.Item.Dat
The shape of the data can be seen in the image in the OP/
Regards
Andy
What is the type of the .Resources field? I thought it was IDictionary?...
ASKER
It's a name value pair, as a child of the main location object. It is ienumerable. If I use intelligence it just says object with no specific type.
Sorry to be vague, all I can do is point you to the image I posted on the OP that shows the main object and resources as a child.
Thanks for your help, Andy
Sorry to be vague, all I can do is point you to the image I posted on the OP that shows the main object and resources as a child.
Thanks for your help, Andy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for sticking with me.
anarki_jimbel I had to modify your sample to get to the data, but the construct for the Dictionary was what I needed:
Dictionary<int, string> resourceList = DataBinder.Eval(e.Item.Dat aItem, "Resources") as Dictionary<int, string>;
And the LINQ worked too to create the string.
Thank you
anarki_jimbel I had to modify your sample to get to the data, but the construct for the Dictionary was what I needed:
Dictionary<int, string> resourceList = DataBinder.Eval(e.Item.Dat
And the LINQ worked too to create the string.
Thank you
Fastest way to convert a list of objects to csv with each object values in a new line