×

Loading...

.net 6 新增的一些有趣的Linq

比如我有如下collections

string[] names = new[] {"tom0", "tom1", "tom2", "tom3", "tom4", "tom5", "tom6"};
int[] items = new[] {0,1,2,3,4,5,6};

我要取倒数第二个items的值(5),.net 6可以用^符号表示反转,区别如下:

items.Reverse().ElementAt(1).Dump(); // old style
items.ElementAt(^2).Dump(); // .net 6

我要取items第2到第5区间的sub collections,可以用 x..y 语法

items.Skip(2).Take(3).Dump(); // old style
items.Take(2..5).Dump(); // .net 6

把items和names结合起来,生成一个新的Tuple<int, string> collection

items.Zip(names).Dump(); // .net 6

Sign in and Reply Report

Replies, comments and Discussions: