zoukankan      html  css  js  c++  java
  • 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);
      }
    }

  • 相关阅读:
    glog Windows Visual Studio 2013 编译项目
    Git Tag管理发行版本
    Ubuntu 16.04环境中读取XBOX 360手柄信息
    GCC 中 的pie和fpie选项
    CMakeLists.txt 常用指令说用
    chrome无法访问github.com
    删除前n天的数据
    shell(9)秒转换为时分秒
    Drools规则引擎实践直白总结
    空闲时间研究一个小功能:winform桌面程序如何实现动态更换桌面图标
  • 原文地址:https://www.cnblogs.com/yuming1983/p/3546364.html
Copyright © 2011-2022 走看看