score:1
Accepted answer
You can use LINQ's DefaultIfEmpty
to insert the empty values that you need when the Sections
collection is empty. See https://msdn.microsoft.com/en-us/library/bb355419(v=vs.110).aspx
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 })))
.ToList();
Source: stackoverflow.com
Related Articles
- Flatten and group complex objects in Linq and preserve null children
- How to flatten nested objects with null leafs with linq expression
- Using Linq to group a list of objects into a new grouped list of list of objects
- How to flatten nested objects with linq expression
- Using LINQ to group a list of objects
- Linq GroupBy with each null value as a group
- Suggestions for designing complex LINQ code
- Linq OrderBy to group objects with the same sets
- Linq to objects when object is null VS Linq to SQL
- LINQ Source Code Available
- Applying LINQ to Objects Group By and Sort By to generic List<T> (C#)
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- C# Linq Query group by check if result is null
- C# LINQ to group by a list of objects in the format as shown below
- LINQ Find Null Objects in List
- How to use Linq to select and group complex child object from a parents list
- LINQ to Entities query to flatten results and select and group desired information
- LINQ to Entities null reference on Union with nested objects
- What is the proper way to use linq Group operators to Flatten Query Results
- sorting list of objects with null properties using linq
- Complex GROUP BY using LINQ
- Linq query to find duplicate objects based on multiple fields AND property is null
- creating Linq to sqlite dbml from DbLinq source code
- Pass LINQ Group By Data Into New List Of Objects
- Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate
- Linq Objects Group By & Sum
- LINQ query returning null for nested objects in Entity DB
- How to flatten a multi level XML into a single level XML using c# code LINQ
- LINQ query with Left Join, Group by without null List
- EF Linq group by ICollection of objects
- C# equivalent of VB LINQ Query
- The specified type member is not supported in LINQ to Entities. Using 0 to many relationship for an entity property
- Error in linq-to-sql: Unable to create a constant value of type
- Linq query mistakes class member for the group keyword
- Why does VB.NET Linq Lambda work one way but not the other
- How to join three tables in linq with child table sum
- LINQ Query is not displaying mt data in my Google chart (Report)
- Group by in a dictionary<string, IList<string>>
- How to add a extra value to the query in c#
- replacing value only from an Xelement using linq
- Linq Statement in Datatable
- Where with IN operator using Linq on NHibernate
- LINQ to XML User-Agent header value
- LINQ left join using Castle Active Records?
- Linq xml query with arguments
- How to use linq to process and conditionally read text file into a List<class> or a dictionary
- Bug: List remains in memory if file deleted, and saves data twice if file is deleted
- How can I join in other tables to my LINQ statement and jget selected columns?
- Populating xml elements representitave of a table structure containing a header and body
- SQL/Linq GroupBy with Min Value and depending column