
LINQ之Last,LastOrDefault
发布日期:2021-05-07 00:14:33
浏览次数:19
分类:技术文章
本文共 6546 字,大约阅读时间需要 21 分钟。
目录
Last()
Last()
可以获取序列的最后一个元素,例如数组或列表。类似List[List.Count - 1]
代码示例:
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<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> 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 < 10 ); // 输出 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],结果:7
由于条件可以用lambda表达式编写,因此可以快速编写简单的条件。
代码示例:
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 = "征史郎" }, }; // ID小于20 Parameter result = parameters.Last( value => value.ID < 20 ); 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:征史郎],结果:ID:13, Name:清次郎
Last()
如果在以下情况下使用,将导致异常。
System.InvalidOperationException序列不包含任何元素
2.当找不到符合条件的元素时,会报错System.InvalidOperationException:序列不包含匹配的元素
LastOrDefault()
如果您不想四处写try
-catch
的话,可以使用LastOrDefault()解决这个问题。
代码示例:
public static class Program{ static void Main( string[] args ) { int[] numbers = new int[] { 1, 2, 3, 5, 7, 11 }; int result = 0; try { // 大于20 result = numbers.LastOrDefault( value => 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还是报错了。 发表评论
最新留言
感谢大佬
[***.8.128.20]2025年03月15日 05时54分18秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
傅里叶变换的初级理解三
2019-03-03
F1 score的意义
2019-03-03
python36+centos7离线安装tensorflow与talib的方法
2019-03-03
hdf5与hdfs的区别
2019-03-03
scala运行的方式
2019-03-03
tf.Session().as_default的作用
2019-03-03
isnull与isna的区别
2019-03-03
python自带超参调优包
2019-03-03
判断python模型是否安装的办法
2019-03-03
xgboost与gbdt的区别
2019-03-03
软件测试中使用coverage统计python代码的覆盖率
2019-03-03
从double到float的强制类型转换
2019-03-03
C++ 任意数据类型转为16进制输出
2019-03-03
PYTHON UDP只能接收本地报文,无法接收其他主机通过路由器发过来的报文
2019-03-03
QLabel控件功能示例
2019-03-03
vue项目中报/sockjs-node/info错误
2019-03-03
如何处理前任程序员留下的代码
2019-03-03
20个非常有用的Java程序片段
2019-03-03
如何锻炼JAVA编程思路?
2019-03-03
全面了解 Nginx 主要应用场景
2019-03-03