I am looking for an elegant solution to aggregate a child collection in a collection into one large collection. My issue is when certain child collections could be null.
EG:
var aggregatedChildCollection = parentCollection.SelectMany(x=> x.ChildCollection);
This throws an exception should any of the child collection objects be null. Some alternatives are:
// option 1
var aggregatedChildCollection = parentCollection
.Where(x=>x.ChildCollection != null)
.SelectMany(x => x.ChildCollection);
// option 2
var aggregatedChildCollection = parentCollection
.SelectMany(x => x.ChildCollection ?? new TypeOfChildCollection[0]);
Both would work but I am doing a certain operation on quite a few child collections on the parent, and it is becoming a bit unweilding.
What I would like is to create an extension method that checks if the collection is null and if so does what option 2 does - adds an empty array. But my understanding of Func is not to a point where I know how to code this extension method. I do know that the syntax I would like is like this:
var aggregatedChildCollection = parentCollection.SelectManyIgnoringNull(x => x.ChildCollection);
Is there a simple extension method that would accomplish this?
Enumerable.Empty<T>()
instead of cutting out the null collections so that I will always have at the very least an empty collection. – Missionary