利用C#怎么获取List集合中的重复值-创新互联
今天就跟大家聊聊有关利用C#怎么获取 List集合中的重复值,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
一、获取集合内重复值
public void GetDuplicateValue() { ListlisA = new List { "A", "B", "C", "A" }; //方式一 借助字典 Dictionary dic = new Dictionary (); lisA.ForEach(x => { if (dic.ContainsKey(x)) dic[x] += 1; else dic[x] = 0; }); List lisDupValues = dic.Where(x => x.Value > 0).Select(x => x.Key).ToList(); //结果{"A"} //方式二 List lisDupValues2 = lisA.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); //结果{"A"} //方式三 等同于方式二 List lisDupValues3 = (from r in lisA group r by r into g where g.Count() > 1 select g.Key).ToList(); //结果{"A"} }
本文题目:利用C#怎么获取List集合中的重复值-创新互联
文章链接:http://azwzsj.com/article/dcijgi.html