Q:

Nested collection items to be displayed in WPF datagrid

I need to display the children (List type) present within a collection (List type) alongwith indexes in a WPF datagrid. 
Below is an example & output to better explain it. 
Example:
public class Quote
{
    public int Id { get; set; }
    public List<Rate> Rates { get; set; }
}

public class Rate
{
    public int Id { get; set; }
    public string AccommodationType { get; set; }
    public decimal Price { get; set; }
}

public class Simple 
{
    public static void Main()
    {
      List<Quote> quotes = new List<Quote> 
      {
        new Quote 
        { 
            Id = 1, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 11, AccommodationType = "A", Price = 1},
                new Rate { Id = 12, AccommodationType = "B", Price = 2}
            } 
        },
        new Quote 
        { 
            Id = 2, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 21, AccommodationType = "C", Price = 3},
                new Rate { Id = 22, AccommodationType = "D", Price = 4},
                new Rate { Id = 23, AccommodationType = "E", Price = 5}
            } 
        },
          new Quote 
        { 
            Id = 3, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 31, AccommodationType = "F", Price = 6},
                new Rate { Id = 32, AccommodationType = "G", Price = 7},
                new Rate { Id = 33, AccommodationType = "H", Price = 8}
            } 
        }
      };
    }
}

Output:
| QuoteID | RateIndex | RateID | AccommodationType | Price |

| 1       | 0         | 11     | A                 | 1     |

| 1       | 1         | 12     | B                 | 2     |

| 2       | 0         | 21     | C                 | 3     |

| 2       | 1         | 22     | D                 | 4     |

| 2       | 2         | 23     | E                 | 5     |

| 3       | 0         | 31     | F                 | 6     |

| 3       | 1         | 32     | G                 | 7     |

| 3       | 2         | 33     | H                 | 8     |

Note: RateIndex is just the index within one Quote.  May sound weird to have the index but that is a need I can't forego. 
What I have tried:
Create another collection of the below class:
public class FormattedCollection
{
    int quoteID;
    int rateIndex;
    Rate rate;
}

I'm populating this collection by iterating over each Quote & then each Rate. 
This collection then becomes the ItemsSource for the datagrid. 
I'm not sure but is there a better way to achieve this? 
Perhaps using LINQ or any other approach? 

A:

You can flatten your quotes list using Linq SelectMany()
Create a new class to combine both Quote and Rate
public class QuoteRate
{
    private Quote _quote;
    private Rate _rate;

    public QuoteRate(Quote quote, Rate rate)
    {
        _quote = quote;
        _rate = rate;
    }

    public int QuoteID => _quote. Id;
    public int RateIndex => _quote. Rates. IndexOf(_rate);
    public int RateID => _rate. Id;
    public string AccommodationType => _rate. AccommodationType;
    public Decimal Price => _rate. Price;
}

Then flatten the quotes collection
var allQuoteRates = quotes. SelectMany(q => q.Rates, (q, r) => new QuoteRate(q, r)).ToList();

You can then use this flatenned list as the ItemsSource for your datagrid. 
It's also possible to use an anonymous type, which saves the effort of creating a new type
quotes. SelectMany(q => q.Rates, (q, r) => new { Quote = q, Rate = r, RateIndex = q.Rates. IndexOf(r) }).ToList();

although this makes the binding for the datagrid a little more complicated

