开发者

Why is Null an Invalid LINQ projection?

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))
开发者_JS百科        ? 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()")?


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;


Make that:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜