Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Null an Invalid LINQ projection?

Tags:

c#

linq

I had the following statement, which always returns null:

var addins = allocations.SelectMany(
        set => set.locations.Any(q => q.IsMatch(level, count))
        ? (List<string>)set.addins : null
     );

I changed it slightly, and now it works fine:

var addins = allocations.SelectMany(
        set => set.locations.Any(q => q.IsMatch(level, count))
        ? set.addins : new List<string>()
     );

My primary question: Why can't null serve as a return type from the ternary operator in this context of LINQ?

A secondary question: Is there a more clever way to formulate the above query (particularly if it eliminates the "new List()")?

like image 863
Brent Arias Avatar asked Sep 22 '10 02:09

Brent Arias


2 Answers

Enumerable.SelectMany will try to enumerate over the sequence returned by your lambda, and it throws a NullReferenceException trying to call GetEnumerator() on null. You need to supply an actual empty sequence. Rather than create a new list, you could use Enumerable.Empty:

var addins = allocations.SelectMany(
    set => set.locations.Any(q => q.IsMatch(level, count))
    ? (List<string>)set.addins : Enumerable.Empty<string>()
    );

I suspect what you actually want is to just call Where before SelectMany to filter out the sets you don't want:

var addins = allocations
    .Where(set => set.locations.Any(q => q.IsMatch(level, count)))
    .SelectMany(set => (List<string>)set.addins);

Or, in query syntax:

var addins =
    from set in allocations
    where set.locations.Any(q => q.IsMatch(level, count))
    from addin in (List<string>)set.addins
    select addin;
like image 134
Quartermeister Avatar answered Nov 12 '22 17:11

Quartermeister


Make that:

(List<string>)set.addins : (List<string>)null

like image 21
KristoferA Avatar answered Nov 12 '22 18:11

KristoferA