利用C#怎么获取List集合中的重复值-创新互联
                                            今天就跟大家聊聊有关利用C#怎么获取 List集合中的重复值,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

一、获取集合内重复值
public void GetDuplicateValue()
{
  List lisA = 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://www.scyingshan.cn/article/dcijgi.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 