Solved

Convert object data to CSV for display.

Posted on 2016-08-05
8
39 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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