Link to home
Start Free TrialLog in
Avatar of Andy Green
Andy GreenFlag for United Kingdom of Great Britain and Northern Ireland

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
Avatar of jitendra patil
jitendra patil
Flag of India image

Avatar of Andy Green

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
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
String names = string.Join(", ", myDict.Select(x => x.Key + "-" + x.Value));

Open in new window

or
string s = string.Join(";", myDict.Select(x => x.Key + "=" + x.Value).ToArray());

Open in new window

or simply
myDict.Select(x => x.Key + "=" + x.Value).ToArray();

Open in new window

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.DataItem, "Resources") as System.Collections.Generic.Dictionary<string, string>;

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?...
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
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.DataItem, "Resources") as Dictionary<int, string>;  

And the LINQ worked too to create the string.

Thank you