Check whether an array is a subset of another

后端 未结 10 1070
走了就别回头了
走了就别回头了 2020-11-22 16:17

Any idea on how to check whether that list is a subset of another?

Specifically, I have

List t1 = new List { 1, 3, 5 };
L         


        
10条回答
  •  我在风中等你
    2020-11-22 17:11

    Building on the answers from @Cameron and @Neil I wrote an extension method that uses the same terminology as the Enumerable class.

    /// 
    /// Determines whether a sequence contains the specified elements by using the default equality comparer.
    /// 
    /// The type of the elements of source.
    /// A sequence in which to locate the values.
    /// The values to locate in the sequence.
    /// true if the source sequence contains elements that have the specified values; otherwise, false.
    public static bool ContainsAll(this IEnumerable source, IEnumerable values)
    {
        return !values.Except(source).Any();
    }
    

提交回复
热议问题