16

I have two arrays idxListResponse & _index both of which have the same structure.

Each of these arrays contains a number of elements with different properties one of which is a child array called indexdata

Each element of this array has a number of properties one of which is another array called datafield. This has a number of key value pair properties.

So in essence I have a hierarchy of 3 separate arrays.

I'm looking to get the first level of the hierarchy + all elements of the 2nd level where the 3rd level items don't match, i.e. exclude only those items from the 2nd level where the 3rd level items are a match.

I've tried approaching this a number of different ways but so far I'm not getting anywhere, could anyone help.

FYI - here's my latest attempt

var q = idxListResponse.Index.Where(a =>
    a.IndexData.All(b =>
        b.DataField.All(c =>
            _index.Index.Where(z =>
                z.IndexData.All(y => y.DataField.Contains(c.name))
            )
        )
    )
);

1 Answer 1

48

Except is a good way of doing that:

var items = source1.Except(source2);

Would return all items in source1 except those in source2.

Since your collections appear to be different types, you would do something like:

source1.Except(source2.Select(s => /* selector here */))

Or you could create your own implementation of IEqualityComparer and use that to compare the two different types.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks but just can't get that working. The issue is that if 1 of the properties of an element in the 3rd array matches then that seems to be getting dropped. I need all the properties for each element in the third array to match a corresponding entry before it can be removed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.