Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine which elements in a list are NOT in another list

Tags:

c#

ilist

I have two IList<CustomObject>, where CustomObject has a Name property that's a string. Call the first one set, and the second one subset. set contains a list of things that I just displayed to the user in a multiselect list box. The ones the user selected have been placed in subset (so subset is guaranteed to be a subset of set, hence the clever names ;) )

What is the most straightforward way to generate a third IList<CustomObject>, inverseSubset, containing all the CustomObjects the user DIDN'T select, from these two sets?

I've been trying LINQ things like this

IEnumerable<CustomObject> inverseSubset = set.Select<CustomObject,CustomObject>(
    sp => !subset.ConvertAll<string>(p => p.Name).Contains(sp.Name));

...based on answers to vaguely similar questions, but so far nothing is even compiling, much less working :P

like image 603
Chuck Wilbur Avatar asked Jan 21 '26 23:01

Chuck Wilbur


1 Answers

Use the LINQ Except for this:

Produces the set difference of two sequences.

like image 83
Oded Avatar answered Jan 23 '26 14:01

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!