Solved

Convert object data to CSV for display.

Posted on 2016-08-05
8
41 Views
Last Modified: 2016-08-09
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
0
Comment
Question by:Andy Green
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:jitendra patil
ID: 41744024
0
 
LVL 3

Author Comment

by:Andy Green
ID: 41744046
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
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41745395
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Andy Green
ID: 41746778
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
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41747880
What is the type of the .Resources field? I thought it was IDictionary?...
0
 
LVL 3

Author Comment

by:Andy Green
ID: 41747960
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
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 41748010
May be, try something like:

Dictionary<int, string> myDict= resourcesList.GetComponents().ToDictionary(x => x.Key, x => x.Value);

Open in new window


or

Dictionary<int, string> myDict= e.Item.DataItem.resources.GetComponents().ToDictionary(x => x.Key, x => x.Value);

Open in new window

0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 41748521
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question