Reputation: 249
I have a List "RootObject" that contains another list "Components". So basically each RootObject could have many Components. I need to get all Distinct values out of "Components" to bind to a ListView.
public class RootObject
{
public string id { get; set; }
public List<string> Components { get; set; }
public string name { get; set; }
}
I think I may need to use SelectMany but not sure how to get them... For example I have my root object into
mylist = deserial.Deserialize<List<RootObject>>(response);
This works. I then need to get a list of Components into a ListView
`ListView.DataSource = //get list of Components`
Upvotes: 2
Views: 561
Reputation: 112602
This gets the distinct components into a list:
var distinctComponents = rootObjects
.SelectMany(r => r.Components)
.Distinct()
.OrderBy(c => c)
.ToList();
I don't know which GUI technology you are using, but a winforms ListView
has no simple binding mechanism. You need to add items and subitems manually. So, you could also drop the ToList()
and enumerate the query directly in a foreach-statement.
Upvotes: 2
Reputation: 56463
for distinct components, you'll need something like this:
List<RootObject> values = new List<RootObject>(); // assuming this is the collection
var distinctComponents = values.SelectMany(r => r.Components).Distinct().ToList();
Upvotes: 0