MoreLinq在Linq的基础上进行了扩展,大致看了下,摘一些记录,以备后需!
一、ForEachExtension
System.Linq只提供了List的ForEach方法,这里扩展了IEnumerable的ForEach方法
IEnumerable<string> lst = students.Select(p => p.Name);
lst.ForEach(x => Console.WriteLine(x));
二、Join扩展 LeftJoinExtension RightJoinExtension FullJoinExtension
var queryLeftJoin = students.LeftJoin(students2,
a => a.Name,
b => b.Name,
a => new { LeftName = a.Name, RightName = "没找到用户" },
(a, b) => new { LeftName = a.Name, RightName = b.Name });
queryLeftJoin.ForEach(x => {
Console.WriteLine($"{x.LeftName}-{x.RightName}");
});
var queryRightJoin = students.RightJoin(students2,
a => a.Name,
b => b.Name,
b => new { LeftName = "没找到用户", RightName = b.Name },
(a, b) => new { LeftName = a.Name, RightName = b.Name });
queryRightJoin.ForEach(x => {
Console.WriteLine($"{x.LeftName}-{x.RightName}");
});
var queryFullJoin = students.FullJoin(students2,
a => a.Name,
b => b.Name,
a => new { LeftName = a.Name, RightName = "没找到用户" },
b => new { LeftName = "没找到用户", RightName = b.Name },
(a, b) => new { LeftName = a.Name, RightName = b.Name });
queryFullJoin.ForEach(x => {
Console.WriteLine($"{x.LeftName}-{x.RightName}");
});
三、InsertExtension 从前插入列表; BacksertExtension 从后插入列表
对于无序加入新的列表,还是挺方便的
var InsertTest = new string[] { "a", "b", "c" }.Insert(new string[] { "d" }, 1); //a,d,b,c
var BackInsertTest = new string[] { "a", "b", "c" }.Backsert(new string[] { "d" }, 1); //a,b,d,c
四、ToDataTableExtension 转为数据表
var dt = students.ToDataTable();
五、ShuffleExtension 打乱数据
以一抽奖为例:
//Demo:抽奖 一等奖1个,二等级奖3个
var players = new List<string>() { "刘备", "关羽", "张飞", "赵云", "马超", "黄忠", "魏延", "关平", "关兴", "小兵甲", "小兵乙", "小兵丙", "小兵丁" };
var bonus1 = players.Shuffle().First();
Console.WriteLine("一等奖:" bonus1);
players.Remove(bonus1); //去掉已抽一等奖的人
var bonus2 = players.Shuffle().Take(3).ToList(); //一定要ToList,不然每调用一次都是重新洗牌
Console.WriteLine("二等奖:" String.Join(",", bonus2.ToArray()));
bonus2.ForEach(x => players.Remove(x)); //去掉已抽二等奖的人
var bonus3 = players.Shuffle().Take(5).ToList();
Console.WriteLine("三等奖:" String.Join(",", bonus3.ToArray()));
六、CountByExtension 分组统计
//按指定的元素,统计对的数据,类似group分组
var countByResult = new string[] { "a","a", "aa" }.CountBy(p => p);
countByResult.ForEach(x=>Console.WriteLine(x.Key "-" x.Value)); //a-2,aa-1
七、PermutationsExtension 排列; SubsetsExtension 组合;
//排列 Permutations
var set = new[] { "a","b","c" };
set.Permutations().ForEach((x,index) => {
Console.WriteLine($"第{index 1}种排列组合");
x.ForEach(y => { Console.WriteLine(y); });
});
//组合 Subsets
var set = new[] { "a", "b", "c" }.Subsets();
set.ForEach((x, index) => {
Console.WriteLine($"第{index 1}种组合");
x.ForEach(y => { Console.WriteLine(y); });
});
八、BatchExtension (每批次数量) 分批; PartitionExtension 分区 按条件分区
//第一批 1,2,3 第二批4,5,6 第三批7,8,9
new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.Batch(3).ForEach((x,index) => {
Console.WriteLine($"第{index 1}批");
x.ForEach(y => {
Console.WriteLine(y);
});
});
//True 0,2,4,6,8 False 1,3,5,7,9
var result = Enumerable.Range(0, 10)
.Partition(x => x % 2 == 0);
result.False.ForEach(x=> {
Console.WriteLine(x);
});
九、StartsWithExtension; EndsWithExtension;
var result1 = new string[] { "a", "b", "c" }.StartsWith(new string[] { "a", "b" }); //true
var result2 = new string[] { "a", "b", "c" }.EndsWith(new string[] { "b", "c" }); //true
十、Evaluate 解方程式、运算
var factories = new Func<int>[]
{
() => -2,
() => 4 1,
() => int.MaxValue,
() => int.MinValue,
};
var results = factories.Evaluate();
results.ForEach(x => Console.WriteLine(x));
十一、FillForwardExtension 向前填充,FillBackwardExtension 向后填充
//FillForward 如果有空值,取左边第一个有效值
//FillBackward 如果有空值,取右边第一个有效值
int? na = null;
var input = new[] { na, 1, 2, na, 3, 4, na };
var result1 = input.FillForward();
var result2 = input.FillBackward();
result1.ForEach(x => Console.WriteLine(x)); //"",1,2,2,3,4,4
result2.ForEach(x => Console.WriteLine(x)); //1,1,2,3,3,4,""
十二、IndexExtension 加上下标 ,形成一个新的键值对
const string one = "one";
const string two = "two";
const string three = "three";
var result = new[] { one, two, three }.Index();
result.ForEach(x => { Console.WriteLine(x); }); //[0,"one"]
十三、PadExtension 从后面填充,PadStartExtension 从前面填充
var result1 = new[] { 123, 456, 789 }.Pad(4); //123,456,789,0
var result2 = new[] { 123, 456, 789 }.PadStart(4); //0,123,456,789
十四、PairwiseExtension 按规则结队
var result = new[] { "a", "b", "c", "d" }.Pairwise((x, y) => x y); //ab,bc,cd
十五、RandomSubsetExtension 随机取N个数据生成子集
Enumerable.Range(1, 10).RandomSubset(4, new Random())
.ForEach(x => { Console.WriteLine(x); });
十六、SliceExtension;截取一段序列
Enumerable.Range(1, 100).Slice(10, 10);//11,12,13...20
参考:
MoreLinq开源地址
https://github.com/morelinq/MoreLINQ
MoreLinq示例
https://gitee.com/gsbhz/MoreLINQ