C# 两个集合对比获取不同

public class CompareCollection
{
  public List<string> CompareList(List<string> oldList, List<string> newList)
  {
    Dictionary<string, string> dict = new Dictionary<string, string>();
    AddDictionary(dict, oldList);
    AddDictionary(dict, newList);

    return dict.Where(r => r.Value==string.Empty).Select(c => c.Key).ToList();
  }
  private void AddDictionary(Dictionary<string, string> dict, List<string> list)
  {
    foreach (var ls in list)
    {
      if (dict.Keys.Contains(ls))
      {
        dict[ls] = ls;
      }
      else
      {
        dict.Add(ls, string.Empty);
      }
    }
  }
}
class Program
{
  static void Main(string[] args)
  {
    //从数据库读取的数组
    List<string> newlist = new List<string> { "001", "002", "003", "999" };
    //初始化0~999数组
    List<string> oldlist = new List<string>();
    for (int i = 0; i < 1000; i++)
    {
      if (i < 10)
        oldlist.Add("00" + i);
      if (i >= 10 && i < 100)
        oldlist.Add("0" + i);
      if (i >= 100)
        oldlist.Add(i.ToString());
    }
    CompareCollection col = new CompareCollection();
    List<string> list = col.CompareList(oldlist, newlist);
  }
}

原文地址:https://www.cnblogs.com/yuming1983/p/3546364.html