C# 中实现对List中的数据查重操作
一、列表查重操作核心如下
1.1、常用列表的查重操作核心如下:
//查找出列表中的所有重复元素
private static List<string> QueryRepeatElementOfList(List<string> list)
{
List<string> listTmp = new List<string>();
if (list!=null && list.Count>0)
{
listTmp = list.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();
}
return listTmp;
}
//查找出列表中的所有重复元素及其重复次数
private static Dictionary<string, int> QueryRepeatElementAndCountOfList(List<string> list)
{
Dictionary<string,int> DicTmp = new Dictionary<string, int>();
if (list != null && list.Count > 0)
{
DicTmp = list.GroupBy(x => x)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());
}
return DicTmp;
}
1.2、类列表的查重数据操作核心如下
//静态扩展类(实现对类重复内容的操作)
public static class Extention
{
public static IEnumerable<T> GetMoreThanOnceRepeatInfo<T>(this IEnumerable<T> extList, Func<T, object> groupProps) where T : class
{ //返回第二个以后面的重复的元素集合
return extList
.GroupBy(groupProps)
.SelectMany(z => z.Skip(1)); //跳过第一个重复的元素
}
public static IEnumerable<T> GetAllRepeatInfo<T>(this IEnumerable<T> extList, Func<T, object> groupProps) where T : class
{
//返回所有重复的元素集合
return extList
.GroupBy(groupProps)
.Where(z => z.Count() > 1)
.SelectMany(z => z);
}
}
二、使用方法
//货物信息
class GoodsInfo
{
public string Code { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public GoodsInfo()
{
}
public GoodsInfo(string code,string name,string position)
{
this.Code = code;
this.Name = name;
this.Position = position;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine("------------------列表操作-------------------------");
//初始化列表
List<string> listTmp = InitList();
//查找出列表中的所有重复元素
List<string> repeatList = QueryRepeatElementOfList(listTmp);
if (repeatList!=null && repeatList.Count>0)
{
Console.WriteLine("---当前所有的重复元素为:---");
foreach (var item in repeatList)
{
Console.WriteLine(item);
}
}
//查找出列表中的所有重复元素
Dictionary<string, int> repeatEleAndCountDic = QueryRepeatElementAndCountOfList(listTmp);
if (repeatEleAndCountDic != null && repeatEleAndCountDic.Count > 0)
{
Console.WriteLine("---当前所有的重复元素个数为:---");
foreach (var item in repeatEleAndCountDic)
{
Console.WriteLine("重复内容为:"+item.Key+ " 重复次数为:"+item.Value);
}
}
List<int> IntTmp = new List<int> { 10, 20, 30, 20, 50,10 };
int Sum = IntTmp.Sum();
Console.WriteLine("---获取列表的和:{0}",Sum);
double Average = IntTmp.Average();
Console.WriteLine("---获取列表的平均数:{0}", Average);
int Max = IntTmp.Max();
Console.WriteLine("---获取列表的最大值:{0}", Max);
int Min = IntTmp.Min();
Console.WriteLine("---获取列表的最小值:{0}", Min);
List<int> DistinctInfo = IntTmp.Distinct().ToList();
foreach (var item in DistinctInfo)
{
Console.WriteLine("---获取列表的不重复数据:{0}", item);
}
Console.WriteLine();
Console.WriteLine("-------------------类列表操作---------------------------");
//初始化类列表
List<GoodsInfo> goodsInfos = InitGoodsInfoList();
//查询到列表中的所有重复数据
List<GoodsInfo> AllRepeatGoodsInfos = QueryAllRepeatInfoOfList(goodsInfos);
if (AllRepeatGoodsInfos!=null && AllRepeatGoodsInfos.Count>0)
{
Console.WriteLine("---当前【满足(名称与位置相同)条件】所有的重复的货物信息为:---");
foreach (var item in AllRepeatGoodsInfos)
{
Console.WriteLine("编号:{0},名称:{1},位置:{2}",item.Code,item.Name,item.Position);
}
}
//查询到列表中重复一次以上的数据
List<GoodsInfo> RepeatMoreThanOnceGoodsInfos = QueryMoreThanOnceInfoList(goodsInfos);
if (RepeatMoreThanOnceGoodsInfos != null && RepeatMoreThanOnceGoodsInfos.Count > 0)
{
Console.WriteLine("---当前【满足(名称相同)条件】所有的重复一次以上的货物信息为:---");
foreach (var item in RepeatMoreThanOnceGoodsInfos)
{
Console.WriteLine("编号:{0},名称:{1},位置:{2}", item.Code, item.Name, item.Position);
}
}
Console.ReadLine();
}
#region 列表操作
//初始化一个列表
private static List<string> InitList()
{
List<string> listTmp = new List<string>();
string str = "101";
for (int i = 0; i < 3000; i++)
{
string strTmp = str + i;
switch (strTmp)
{
case "10112":
case "10118":
case "10124":
strTmp = "10107";
break;
case "10126":
case "10137":
case "10138":
case "10149":
strTmp = "10111";
break;
default:
break;
}
listTmp.Add(strTmp);
}
return listTmp;
}
//查找出列表中的所有重复元素
private static List<string> QueryRepeatElementOfList(List<string> list)
{
List<string> listTmp = new List<string>();
if (list!=null && list.Count>0)
{
listTmp = list.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();
}
return listTmp;
}
//查找出列表中的所有重复元素及其重复次数
private static Dictionary<string, int> QueryRepeatElementAndCountOfList(List<string> list)
{
Dictionary<string,int> DicTmp = new Dictionary<string, int>();
if (list != null && list.Count > 0)
{
DicTmp = list.GroupBy(x => x)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());
}
return DicTmp;
}
#endregion
#region 类操作
//初始化类列表
private static List<GoodsInfo> InitGoodsInfoList()
{
List<GoodsInfo> goodsInfos = new List<GoodsInfo>()
{
new GoodsInfo("10101","海尔冰箱","0102"),
new GoodsInfo("10102","海尔电视","0103"),
new GoodsInfo("10103","海尔空调","0104"),
new GoodsInfo("10104","海尔净化器","0105"),
new GoodsInfo("10105","海尔冷风机","0106"),
};
GoodsInfo goodsInfo1 = new GoodsInfo("10106", "海尔冰箱", "0102");
GoodsInfo goodsInfo2 = new GoodsInfo();
goodsInfo2.Code = "10108";
goodsInfo2.Name = "海尔净化器";
goodsInfo2.Position = "0108";
goodsInfos.Add(goodsInfo1);
goodsInfos.Add(goodsInfo2);
return goodsInfos;
}
//查询到列表中的所有重复数据(条件为:名称与位置相同)
private static List<GoodsInfo> QueryAllRepeatInfoOfList(List<GoodsInfo> goodsInfos)
{
List<GoodsInfo> goodsInfoTmpList = new List<GoodsInfo>();
if (goodsInfos!=null && goodsInfos.Count>0)
{
goodsInfoTmpList=goodsInfos.GetAllRepeatInfo(x => new { x.Name, x.Position })
.ToList();
}
return goodsInfoTmpList;
}
//查询到重复了一次以上的数据
private static List<GoodsInfo> QueryMoreThanOnceInfoList(List<GoodsInfo> goodsInfos)
{
List<GoodsInfo> goodsInfoTmpList = new List<GoodsInfo>();
if (goodsInfos != null && goodsInfos.Count > 0)
{
goodsInfoTmpList = goodsInfos.GetMoreThanOnceRepeatInfo(x => new { x.Name})
.ToList();
}
return goodsInfoTmpList;
}
#endregion
}//Class_end
三、结果如下:
参考:https://blog.csdn.net/xiaochenXIHUA/article/details/107020130