更加詳細的開發內容請參考: 微軟官方Linq開發文檔
一、列表查重操作核心如下
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
三、結果如下: