LINQ之DefaultIfEmpty

返回LINQ大全首页

DefaultIfEmpty()

如果使用DefaultIfEmpty()的数组或列表为空,则返回元素类型的默认值。
MSDN
public static IEnumerable DefaultIfEmpty( this IEnumerable source );

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[] numbersA  = new int[] { 0, 1, 2 };
        int[] numbersB  = new int[] {};

        IEnumerable<int> numbersAPlus   = numbersA.DefaultIfEmpty();
        IEnumerable<int> numbersBPlus   = numbersB.DefaultIfEmpty();

        System.Console.WriteLine( "numbersA     :{0}", numbersA.Text() );
        System.Console.WriteLine( "numbersAPlus :{0}", numbersAPlus.Text() );
        System.Console.WriteLine( "numbersB     :{0}", numbersB.Text() );
        System.Console.WriteLine( "numbersBPlus :{0}", numbersBPlus.Text() );

        System.Console.ReadKey();
    }

    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }
}

numbersA :[0], [1], [2],
numbersAPlus :[0], [1], [2],
numbersB :
numbersBPlus :[0],

DefaultIfEmpty()可以将为空时返回的值通过参数传递。

MSDN
public static IEnumerable DefaultIfEmpty( this IEnumerable source, TSource defaultValue );

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[] numbersA      = new int[] { 0, 1, 2 };
        int[] numbersB      = new int[] {};
        int   defaultNumber = -1;

        IEnumerable<int> numbersAPlus   = numbersA.DefaultIfEmpty( defaultNumber );
        IEnumerable<int> numbersBPlus   = numbersB.DefaultIfEmpty( defaultNumber );

        System.Console.WriteLine( "numbersA     :{0}", numbersA.Text() );
        System.Console.WriteLine( "numbersAPlus :{0}", numbersAPlus.Text() );
        System.Console.WriteLine( "numbersB     :{0}", numbersB.Text() );
        System.Console.WriteLine( "numbersBPlus :{0}", numbersBPlus.Text() );

        System.Console.ReadKey();
    }

    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }
}

numbersA :[0], [1], [2],
numbersAPlus :[0], [1], [2],
numbersB :
numbersBPlus :[-1],

您还可以在自己的class中使用它,如下所示。

代码示例:

public static class Program
{
    private class Parameter
    {
        public int      ID      { get; set; }
        public int      Age     { get; set; }
        public string   Name    { get; set; }
        
        public override string ToString()
        {
            return string.Format( "ID:{0}, Age:{1}, Name:{2}", ID, Age, Name );
        }
    }

    static void Main( string[] args )
    {
        Parameter[] parametersA = new Parameter[]
        {
            new Parameter() { ID = 0, Age = 52, Name = "正一郎" },
            new Parameter() { ID = 8, Age = 28, Name = "清次郎" },
            new Parameter() { ID = 3, Age = 20, Name = "誠三郎" },
            new Parameter() { ID = 4, Age = 18, Name = "征史郎" },
        };

        Parameter[] parametersB         = new Parameter[] {};
        Parameter   defaultparameter    = new Parameter() { ID = -1, Age = 0, Name = "无名" };

        IEnumerable<Parameter> parametersAPlus  = parametersA.DefaultIfEmpty( defaultparameter );
        IEnumerable<Parameter> parametersBPlus  = parametersB.DefaultIfEmpty( defaultparameter );

        System.Console.WriteLine( "parametersA     :{0}", parametersA.Text() );
        System.Console.WriteLine( "parametersAPlus :{0}", parametersAPlus.Text() );
        System.Console.WriteLine( "parametersB     :{0}", parametersB.Text() );
        System.Console.WriteLine( "parametersBPlus :{0}", parametersBPlus.Text() );

        System.Console.ReadKey();
    }

    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }
} 

parametersA :[ID:0, Age:52, Name:正一郎], [ID:8, Age:28, Name:清次郎], [ID:3
, Age:20, Name:誠三郎], [ID:4, Age:18, Name:征史郎],
parametersAPlus :[ID:0, Age:52, Name:正一郎], [ID:8, Age:28, Name:清次郎], [ID:3
, Age:20, Name:誠三郎], [ID:4, Age:18, Name:征史郎],
parametersB :
parametersBPlus :[ID:-1, Age:0, Name:无名],

你可能感兴趣的:(C#,c#,.net,linq,unity)