int[] Pattern3 = new int[] { 
            3,7, 8, 11, 15, 23, 24 };int[] Pattern4 = new int[] { 
            4,5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 20, 21, 22, 23, 24 };        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<int,int[]> dict = new Dictionary<int,int[]>();            dict.Add(3, Pattern3);
            dict.Add(4, Pattern4);            var d = dict[3].ToList().Except(dict[4]).ToList();            foreach (var item in d)
            {
                Console.WriteLine(item);
            }
        }如果是多个dict怎么比较比较好            dict.Add(5, Pattern5);
          dict.Add(6, Pattern6);
          。。

解决方案 »

  1.   

        private IEnumerable<int> GetExpectedList(InterRobotInfo robot)
            {
                IEnumerable<int> expectedList = null;            var keys = robot.DictPattern.Keys.ToList();            for (int i = 0; i < keys.Count; i++)
                {
                    if (keys.Count == 2)
                    {                  return  expectedList = robot.DictPattern[keys[i]].Except(robot.DictPattern[keys[i + 1]]);
                    }
                    else
                    {
                        expectedList = robot.DictPattern[keys[i]].Except(expectedList);
                    }
                }            return expectedList;
            }
      

  2.   

    记录下前两个的差集,然后将差集与后面每个list比较再取差集
      

  3.   

    想知道 linq lambada 的精简写法
      

  4.   

    linq lambada精简写法
     int[] Pattern3 = new int[] { 
                 3,7, 8, 11, 15, 23, 24 };            int[] Pattern4 = new int[] { 
                 4,5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 20, 21, 22, 23, 24 };            int[] Pattern5 = new int[] { 
                 4,5, 6, 7, 8, 9, 10, 11, 12};            int[] Pattern6 = new int[] { 
                 34,35, 36, 37, 38, 39};
                var dict = new int[][] { Pattern3, Pattern4, Pattern5, Pattern6 };            var res = dict.Aggregate((x, y) => x.Except(y).ToArray<int>());  //取Pattern3,Pattern4,Pattern5,Pattern6的差集
      

  5.   

    用dictionary试了,没成功,y.value是只读的
       Dictionary<int, int[]> dict = new Dictionary<int, int[]>();
                dict.Add(3, Pattern3);
                dict.Add(4, Pattern4);
                dict.Add(5, Pattern5);
                dict.Add(6, Pattern6);
                //取Pattern3,Pattern4,Pattern5,Pattern6的差集
                var res = dict.Aggregate((x, y) => { y.Value= x.Value.Except(y.Value).ToArray<int>(); return y; });  
      

  6.   

    可以稍加修改
     //取Pattern3,Pattern4,Pattern5,Pattern6的差集
                var res = dict.Aggregate((x, y) => { return new KeyValuePair<int, int[]>(y.Key, x.Value.Except(y.Value).ToArray()); }).Value;
              
      

  7.   

    楼上的代码这刚才也是这样写的,但结果不太对,出来两个
    key为6,value为{3,15}的数组,
    虽然每个6,{3,15}都是对的,但感觉总是怪怪的
      

  8.   

    楼主如果不是非得要Dictionary的话,就按8楼那样,用int二维数组吧