سؤال

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

هل كانت مفيدة؟

المحلول

Use the LINQ Except for this:

Produces the set difference of two sequences.

نصائح أخرى

Aha, too much SQL recently - I didn't want Select, I wanted Where:

List<string> subsetNames = subset.ConvertAll<string>(p => p.Name);
IEnumerable<CustomObject> inverseSubset =
    set.Where<CustomObject>(p => !subsetNames.Contains(p.Name));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top