Any idea on how to check whether that list is a subset of another?
Specifically, I have
List<double> t1 = new List<double> { 1, 3, 5 };
List<double> t2 = new List<double> { 1, 5 };
How to check that t2 is a subset of t1, using LINQ?
Any idea on how to check whether that list is a subset of another?
Specifically, I have
List<double> t1 = new List<double> { 1, 3, 5 };
List<double> t2 = new List<double> { 1, 5 };
How to check that t2 is a subset of t1, using LINQ?
Try this
The idea here is that Intersect will only return the values that are in both Arrays. At this point if the length of the resulting set is the same as the original set, then all elements in "set" are also in "check" and therefore "set" is a subset of "toCheck"
Note: My solution does not work if "set" has duplicates. I'm not changing it because I don't want to steal other people's votes.
Hint: I voted for Cameron's answer.
If you are unit-testing you can also utilize the CollectionAssert.IsSubsetOf method :
In the above case this would mean:
Building on the answers from @Cameron and @Neil I wrote an extension method that uses the same terminology as the Enumerable class.
@Cameron's solution as an extension method:
Usage:
(This is similar, but not quite the same as the one posted on @Michael's blog)
eg: