MoreLinq是在Linq的基础上增加了很多的扩充。
Git:
https://github.com/morelinq/MoreLINQ
Nuget:
pm=> Install-Package morelinq -Version 3.3.2
具体使用:
-
ForEach
var list1 = Enumerable.Range(1, 10); list1.ForEach(a => Console.WriteLine(a));
输出:
- Insert(插入-从前往后)/Backsert(后置,从后往前)
var list1 = Enumerable.Range(1, 10); var newInt = list1.Backsert(new List<int> { 99 }, 10);//从后往前数10个 Console.WriteLine("this‘s Backsert:"); newInt.ForEach(a => Console.Write(a)); var insert= list1.Insert(new List<int> { -1 }, 0);//插入第0个 Console.WriteLine("\r\n this‘s Insert"); insert.ForEach(a => Console.Write(a));
结果:
-
Cartesian(笛卡尔积)/Zip(拉链)
var list1 = Enumerable.Range(1, 10); var list2 = Enumerable.Range(10, 10); var CartesianResult = list1.Cartesian(list2, (a, b) => new int[] { a, b }); Console.WriteLine("Cartesian-Result:"); CartesianResult.ForEach(a => Console.Write(string.Join("-", a)+" ")); var ZipResult = list1.Zip(list2, (a, b) => new int[] { a, b }); Console.WriteLine("\r\nZip-Result:"); ZipResult.ForEach(a => Console.Write(string.Join("-",a) + " "));
结果:
-
Batch(批量)/Partition(分区)
var list1 = Enumerable.Range(1, 10); var result1 = list1.Batch(3); Console.WriteLine("Batch:"); result1.ForEach((a,i) => { Console.WriteLine($"this {++i} Batch: "); a.ForEach(v => Console.WriteLine(v)); }); var result2 = list1.Partition(a=>a%2==1); Console.WriteLine("分割线---------------------------------------"); Console.WriteLine("Partition-false:"); result2.False.ForEach(a => Console.Write(a + " ")); Console.WriteLine("\r\nPartition-true:"); result2.True.ForEach(a => Console.Write(a + " "));
结果:
-
Shuffle(随机排序/乱序)
var list1 = Enumerable.Range(1, 10); list1.Shuffle().ForEach(a => Console.WriteLine(a));
结果:
-
OfType<T>(按类型获取集合)
ArrayList list = new ArrayList() { 1, "a", new { name="张三"}, 3.02, 'A', false, true, 999999999999999999L }; {// # C#9.0语法糖 //foreach (var item in list) //{ // if (item is { } ai and not string and not char and not 0) // // { // Console.WriteLine( item); // } //} } Console.WriteLine("type of long:"); Console.WriteLine(" -"+ list.OfType<long>().FirstOrDefault()); Console.WriteLine("type of bool:"); Console.WriteLine(" -" + list.OfType<bool>().FirstOrDefault()); Console.WriteLine("type of int:"); Console.WriteLine(" -" + list.OfType<int>().FirstOrDefault());
结果:
-
ToDataTable(集合转表)
class Program { class Person { public Person(string name, int age) { Name = name; Age = age; } public string Name { get; set; } public int Age { get; set; } } static void Main(string[] args) { List<Person> people = new List<Person>(); people.Add(new Person("张三", 15)); people.Add(new Person("李四", 16)); people.Add(new Person("王五", 17)); people.Add(new Person("赵六", 18)); var data= people.ToDataTable(); } }
结果:
-
ToDictionary(转换为字典)
List<string> list = new () { "张三","男","18","false", "李四","女","10","false", "王五","女","29","true", "赵六","女","57","true" }; var dic= list.Batch(4).ToDictionary( a => a.ElementAt(0).Trim(), a => int.Parse(a.ElementAt(2)) ); dic.ForEach(i => Console.WriteLine($"key:{i.Key} value:{i.Value}"));
结果:
更多精彩,自己探索: