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

    0关注

    193博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

LINQ之Last,LastOrDefault

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

返回LINQ大全首页

目录
  • Last()
  • LastOrDefault()

Last()

Last()可以获取序列的最后一个元素,例如数组或列表。类似List[List.Count - 1] MSDN

代码示例:

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

        int result  = numbers.Last();

        //输出
        System.Console.WriteLine( "数据:{0}", numbers.Text() );
        System.Console.WriteLine( "结果:{0}", result );

        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;
    }
} 
数据:[1], [2], [3], [5], [7], [11],
结果:11

我们可以指定Last()的获取条件。获得满足条件的最后一个元素。 public static TSource Last( this IEnumerable source, Func predicate );

代码示例:

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

        // 小于10的值
        int result  = numbers.Last( value => value  value.ID  value > 20 );
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "异常:{0}", i_exception );

            System.Console.ReadKey();
            return;
        }        

        System.Console.WriteLine( "数据:{0}", numbers.Text() );
        System.Console.WriteLine( "结果:{0}", result );

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

} 

数据:[1], [2], [3], [5], [7], [11],
结果:0

int该类型的默认值为0,因此返回0。 下面尝试下列表为空的情况。

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[] numbers = new int[] { };

        int result = 0;
        try
        {
            result = numbers.LastOrDefault();
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "异常:{0}", i_exception );

            System.Console.ReadKey();
            return;
        }        

        System.Console.WriteLine( "数据:{0}", numbers.Text() );
        System.Console.WriteLine( "结果:{0}", result );

        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;
    }
} // class Program
数据:
结果:0

在这些情况下,将无一例外的返回0。 让我们也尝试一下class类型。

代码示例:

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

        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 =  5, Name = "正一郎" },
            new Parameter() { ID = 13, Name = "清次郎" },
            new Parameter() { ID = 25, Name = "誠三郎" },
            new Parameter() { ID = 42, Name = "征史郎" },
        };

        Parameter result = new Parameter();
        try
        {
            // 大于50
            result = parameters.LastOrDefault( value => value.ID > 50 );
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "异常:{0}", i_exception );

            System.Console.ReadKey();
            return;
        }        

        System.Console.WriteLine( "数据:{0}", parameters.Text() );
        System.Console.WriteLine( "结果:{0}", result );

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

}
数据:[ID:5, Name:正一郎], [ID:13, Name:清次郎], [ID:25, Name:誠三郎], [ID:42, Name:征史郎],
结果:

class默认值为null,所以结果为空。

LastOrDefault()似乎更方便,但是有一些要注意的地方。 比如说int类型返回0的情况,你无法判断是找到了0还是报错了。

关注
打赏
1648518768
查看更多评论
立即登录/注册

微信扫码登录

0.3084s