Sinaesthetic
Sinaesthetic

Reputation: 12192

How do I group by an ID within a child collection

I have a collection of A. A contains several properties, including a collection of B. B contains an ID. Using just the collection of A, how do I group by B.ID ??

A bit more detail:

public class Config
{
    public int Id { get; set; }
    public List<Feature> Features { get; set; }
}

public class Feature
{
    public string Title { get; set; }
    public int Id { get; set; }
}

If I had a collection of Config, I need to group by Feature.Id so that I had a result that looked something like this:

- Feature 1
    - Config1
        -Id
        -Features
    - Config2
        -Id
        -Features
    - Config3
        -Id
        -Features
- Feature 2
    - Config1
        -Id
        -Features
    - Config2
        -Id
        -Features
    - Config3
        -Id
        -Features
- Feature 3
    - Config1
        -Id
        -Features
    - Config2
        -Id
        -Features
    - Config3
        -Id
        -Features

How might I achieve that using LINQ?

Upvotes: 0

Views: 41

Answers (1)

AD.Net
AD.Net

Reputation: 13399

listConfigs.SelectMany(c=>c.Features).GroupBy(f=>f.Id)

Upvotes: 2

Related Questions