简体   繁体   中英

DefaultIfEmpty doesn't work

Why is array still null after queried by DefaultIfEmpty ?

class Program
{
    static void Main(string[] args)
    {
        Program[] array = new Program[5];
        Program[] query = array.DefaultIfEmpty(new Program()).ToArray();
        foreach (var item in query)
        {
            Console.WriteLine(item.ToString());
        }
        Console.ReadKey();
    }
}

Your array isn't empty - it's got 5 elements, each of which has a null value.

Note that array itself isn't null - and neither is query . But each element of them is null.

A truly empty array would have 0 elements:

using System;
using System.Linq;

class Program
{

    static void Main(string[] args)
    {
        Program[] array = new Program[0];
        Program[] query = array.DefaultIfEmpty(new Program()).ToArray();
        foreach (var item in query)
        {
            Console.WriteLine(item.ToString());
        }
        Console.ReadKey();
    }
}

Another alternative would be to filter null elements while you're copying the array:

Program[] query = array.Where(x => x != null)
                       .DefaultIfEmpty(new Program())
                       .ToArray();

EDIT: Perhaps you misunderstood the purpose of DefaultIfEmpty ? This works on the whole sequence , not on an individual element basis. The idea isn't to replace null elements with a default value; it's to make sure that the result sequence is never empty. If you want to do a simple "replace null with a new value" you can do:

Program[] query = array.Select(x => x ?? new Program())
                       .ToArray();

Note that this will create a new value of Program for each null element; if you only want to create a single instance of Program and use multiple references to that, one for each originally null element, you could do this:

Program defaultValue = new Program();
Program[] query = array.Select(x => x ?? defaultValue)
                       .ToArray();

听起来你只是想要:

var query = Array.ConvertAll(array, item => item ?? new Program());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM