Linq IEnumerable<IGrouping<string, Class>> back to List<Class>

c#, linq

Solution

Each `IGrouping<string, DocumentData>` is an `IEnumerable<DocumentData>`, so you could simply call `SelectMany` to flatten the sequences:

var list = documents.SelectMany(d => d).ToList();

Edit: Per the updated question, it seems like the OP wants to select just the first document for any given filename. This can be achieved by calling `First()` on each `IGrouping<string, DocumentData>` instance:

IEnumerable<DocumentData> documents = 
    documentCollection.GroupBy(g => g.FileName, StringComparer.OrdinalIgnoreCase)
                      .Select(g => g.First())
                      .ToList();

Problem

How can I turn the following statement back to `List<DocumentData>` ``` IEnumerable<IGrouping<string, DocumentData>> documents = documentCollection.Select(d => d).GroupBy(g => g.FileName); ``` the goal is to get List that should be smaller than documentCollection. FileName contains duplicates so I want to make sure I don't have duplicate names. I have also tried the following but it's still providing me with duplicate file names ``` documentCollection = documentCollection.GroupBy(g => g.FileName).SelectMany(d => d).ToList(); ```

Original source

Related problems