DenverCoder9 DenverCoder9 - 4 months ago 31
C# Question

C# LINQ SelectMany with Default

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.


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?


You can use a custom extension method:

public static IEnumerable<TResult> SelectManyIgnoringNull<TSource, TResult>(
    this IEnumerable<TSource> source, 
    Func<TSource, IEnumerable<TResult>> selector)
    return source.Select(selector)
        .Where(e => e != null)
        .SelectMany(e => e);

And use like this:

var aggregatedChildCollection = parentCollection
    .SelectManyIgnoringNull(x => x.ChildCollection);