List<int> a1 = {1,2,3};List<int> a2 = {1,2,3,4,5,6,7};如果a1全包含在a2中则返回true;求最简单的代码

解决方案 »

  1.   

    List<int> a1 = new List<int>(){ 1, 2, 9 };                List<int> a2 = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
                    if (a1.Intersect(a2).Count() == a1.Count())
                    {
                        //包含
                    }
      

  2.   


            public static bool ContainsAll<T>(List<T> container, List<T> subset){
                return container.Intersect(subset).Count() == subset.Count;
            }
      

  3.   

    if (a1.Except(a2).Count() == 0)
      

  4.   

    或者
    a1.All(x => a2.Contains(x)) //这个最容易看懂:a1中全部(all)元素都包括在(contains)a2中