Reputation: 4131
Class definitions:
public class Site
{
public Site(){}
public string SiteId{get;set;}
public int Value{get;set;}
}
public class HubObject
{
public HubObject(){}
public Site Hub{get;set;}
public List<Site> Sites{get;set;}
}
My Dictionary<int, HubObject> result
contains..
Key Value
0 {Hub={"Hub0", 600}, Sites={{"Chicago", 100}, {"Minneapolis", 200}, {"Newyork", 300}}}
1 {Hub={"Hub1", 300}, Sites={{"Portland", 100}, {"Santaclara", 200}}}
want to convert it to List<List<string>>
in below format.
"SiteId", "Value"
"Hub0", 600
"Chicago", 100
"Minneapolis", 200
"Newyork", 300
"Hub1", 300
"Portland", 100
"Santaclara", 200
This is what I have achieved so far
var sitesList = result.SelectMany(r => r.Value.Sites.Select(x => new
{
x.SiteId,
x.Value
})).ToList();
This gives me...
"SiteId", "Value"
"Chicago", 100
"Minneapolis", 200
"Newyork", 300
"Portland", 100
"Santaclara", 200
How do I add "Hub" rows to this? Like, first Hub0
parent followed by all sites that it belongs to it and so on.
Upvotes: 1
Views: 106
Reputation: 31249
I think that you can do like this:
var sitesList=result.SelectMany(r =>
{
return r.Value.Sites
.Concat(new List<Site>{r.Value.Hub})
.Select (s =>new{s.SiteId,s.Value});
});
You can even do this as well:
var sitesList=result.SelectMany(r =>
new List<Site>{r.Value.Hub}.Concat(r.Value.Sites)
.Select (s =>new{s.SiteId,s.Value}));
Upvotes: 2