您当前的位置: 首页 > 
  • 4浏览

    0关注

    193博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

LINQ之All

我寄人间雪满头丶 发布时间:2020-04-20 21:15:16 ,浏览量:4

返回LINQ大全首页

All()

使用All()可以判断序列中的所有元素(例如数组或列表)是否满足指定条件。 MSDN

public static bool All( this IEnumerable source, Func predicate );

以下代码使用一个lambda表达式来描述要检查的条件。

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[]   numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        bool reaultA = numbers.All( value => value % 2 == 0 );
        bool reaultB = numbers.All( value => value  value >= 5 );

        System.Console.WriteLine( "输出:{0}", numbers.Text() );
        System.Console.WriteLine( "全是偶数  :{0}", reaultA );
        System.Console.WriteLine( "小于10:{0}", reaultB );
        System.Console.WriteLine( "大于等于5 :{0}", reaultC );

        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;
    }
}

输出:[0], [1], [2], [3], [4], [5], [6], [7], [8], [9],
全是偶数 :False
小于10:True
大于等于5 :False

那么在空的数组中使用All()会发生什么呢?让我们尝试一下。

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[]   numbers = new int[] {};
        
        bool reaultA = numbers.All( value => value % 2 == 0 );
        bool reaultB = numbers.All( value => value  value >= 5 );

        System.Console.WriteLine( "输出:{0}", numbers.Text() );
        System.Console.WriteLine( "全是偶数  :{0}", reaultA );
        System.Console.WriteLine( "小于等于10:{0}", reaultB );
        System.Console.WriteLine( "大于等于5 :{0}", reaultC );

        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;
    }
} 

输出:
全是偶数 :True
小于等于10:True
大于等于5 :True

由此可知,在序列为空时所有条件都会返回true。 为什么为空数组时返回True?让我们做更多的研究。 在下面的类中,GetNumber()函数会在调用时输出日志,让我们进一步了解All()

代码示例:

public static class Program
{
    private class Parameter
    {
        public int      ID      { get; set; }
        public string   Name    { get; set; }

        public int GetNumber()
        {
            System.Console.WriteLine( "我{0} 数值为{1} ", Name, ID );
            return ID;
        }

        public override string ToString()
        {
            return string.Format( "ID:{0}, Name:{1}", ID, Name );
        }
    }

    static void Main( string[] args )
    {
        Parameter[] parameters = new Parameter[]
        {
            new Parameter() { ID = 1, Name = "正一郎" },
            new Parameter() { ID = 2, Name = "清次郎" },
            new Parameter() { ID = 3, Name = "誠三郎" },
            new Parameter() { ID = 4, Name = "征史郎" },
        };

        // 全部小于10
        bool reault = parameters.All( value => value.GetNumber()  value.GetNumber()             
关注
打赏
1648518768
查看更多评论
0.1020s