Select a row that is not a property of another object

I am writing code that will select string keys from an ApiIds array that are not ApiId properties of the result objects.

I wrote the following code, but it looks superfluous for me, is there a way to combine it into one statement and not convert HashSet objects to another HashSet of strings?

        var resultsCached = new HashSet<string>(results.Select(x => x.ApiId));
        var missingResults = apiIds.Select(x => !resultsCached.Contains(x));

Thank.

+4
source share
2 answers

Except will provide you with items that are not in another collection:

var missingResults = apiIds.Except(results.Select(x => x.ApiId));
+8
source

Another effective O (n) method is to use HashSet.ExceptWith, which removes all the elements from the set that are in the second sequence:

HashSet<string> apiIdSet = new HashSet<string>(apiIds);
apiIdSet.ExceptWith(results.Select(x => x.ApiId));  

, results.

+1

All Articles