Solved

Convert object data to CSV for display.

Posted on 2016-08-05
8
36 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 29

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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 29

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 29

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now