Determining If One List Contains All Elements of Another in .NET
The LINQ extension method Except can be employed to ascertain whether a list contains all elements of another list. By contrasting the two lists, we can determine if the first list lacks any elements present in the second. This approach is particularly convenient in .NET 3.5 or later versions.
public static class LinqExtras // Or whatever { public static bool ContainsAllItems<T>(this IEnumerable<T> a, IEnumerable<T> b) { return !b.Except(a).Any(); } }
This extended ContainsAllItems method operates generically for any sequence type(IEnumerable
In summary, the ContainsAllItems method provides a concise and efficient mechanism for verifying the presence of all elements from one list within another, leveraging the capabilities of LINQ to simplify this common operation.
The above is the detailed content of How Can I Check If One List Contains All Elements of Another in .NET?. For more information, please follow other related articles on the PHP Chinese website!