C # linq - get elements from an array that don't exist in another array

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

Each of these arrays contains several elements with different properties, one of which is a child array called indexdata p>

Each element of this array has a number of properties, one of which is another array, called a data field. This has several key value pair properties.

So, essentially, I have a hierarchy of 3 separate arrays.

I want to get the first level of the hierarchy + all the elements of the 2nd level, where the elements of the 3rd level do not match, i.e. exclude only those elements from the 2nd level, where the elements of the third level correspond.

I tried to approach this in several different ways, but so far I'm not going anywhere, anyone can help.

FYI - this is my last 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)) ) ) ) ); 
+6
source share
1 answer

Except is a good way:

 var items = source1.Except(source2); 

source1 all elements in source1 , except those specified in source2 .

Since your collections look different types, you would do something like:

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

Or you can create your own implementation of IEqualityComparer and use it to compare two different types.

+23
source

Source: https://habr.com/ru/post/906686/


All Articles