list_old={1,2,3,4};
list_new={2,3,5,7,8};要的结果:
list_new中的5,7,8对于list_old是新增的,
list_old中的1,4在list_new中式不存在的。求比较高效的方法,语言或代码描述。目前思路,
foreach nid in list_new
{
if(nid not in list_old) nid add to 新增的;
}
foreach oid in list_old
{
if(oid not in list_new) oid add to 不存在的;
}

解决方案 »

  1.   

    linq
    int[] list_old={1,2,3,4};
    int[] list_new = { 2, 3, 5, 7, 8 };
    int[] except = list_new.Except(list_old).ToArray();
      

  2.   

    先从源头着手有没有高效方法,比如ILIST的生成或是数据是否是有规律的,比如有顺序,如果都没有,那就是集合比较交集
      

  3.   


    int[] list_old = { 1, 2, 3, 4 };
    int[] list_new = { 2, 3, 5, 7, 8 };
    int[] append = list_new.Except(list_old).ToArray();//new
    int[] removed = list_old.Except(list_new).ToArray();//removed
    Console.WriteLine("新增:");
    append.ToList().ForEach(n => Console.Write(n+"\t"));
    Console.WriteLine(); 
    Console.WriteLine("删除:");
    removed.ToList().ForEach(n => Console.Write(n+"\t"));
    结果:新增:
    5       7       8
    删除:
    1       4
      

  4.   


    IList<int> list_old = new List<int>();
            IList<int> list_new = new List<int>();        list_old.Add(1);
            list_old.Add(2);
            list_old.Add(3);
            list_old.Add(4);        list_new.Add(2);
            list_new.Add(3);
            list_new.Add(5);
            list_new.Add(7);
            list_new.Add(8);        IEnumerable<int> list_intersect=list_old.Intersect(list_new);
            IEnumerable<int> list_old_except_new = list_old.Except(list_new);
            IEnumerable<int> list_new_except_old = list_new.Except(list_old);        string Result = "";
            Result += "list_old:1,2,3,4<br />";
            Result += "list_new:2,3,5,7,8<br />";
            Result += "交集为:<br />";
            foreach (int n in list_intersect)
            {
                Result += n.ToString() + ",";
            }
            Result += "<br />list_old_except_new:<br />";
            foreach (int n in list_old_except_new)
            {
                Result += n.ToString() + ",";
            }
            Result += "<br />list_new_except_old:<br />";
            foreach (int n in list_new_except_old)
            {
                Result += n.ToString() + ",";
            }
            Response.Write(Result);result:
    list_old:1,2,3,4
    list_new:2,3,5,7,8
    交集为:
    2,3,
    list_old_except_new:
    1,4,
    list_new_except_old:
    5,7,8, That's OK!