Accepted answer

You can use LINQ's DefaultIfEmpty to insert the empty values that you need when the Sections collection is empty. See

On the end of the second.SelectMany(...) add:

.DefaultIfEmpty(new { rt, null, null, cystNum })

Which does 'for each route that has no sections, create a dummy result'.

EDIT: Given your changed requirement it would look like this:

 var res = data.SelectMany(r => r.CustomerNumbers
            .SelectMany(c => r.Sections.Where(s => s.CustomerNumbers.Contains(c))
                .SelectMany(s => s.Sequences
                  .Select(seq => new { CustomerNumber = c, Route = r, Section = s, Sequence = seq }))
                .DefaultIfEmpty(new { CustomerNumber = c, Route = r, Section = (SectionExport)null, Sequence = (SequenceExport)null })))

Related Articles