C#LINQ - 获得数组元素不以不同的阵列存在 [英] c# linq - get elements from array which do not exist in a different array
问题描述
我有两个数组idxListResponse&安培; _index其中两者具有相同的结构。
每个这些阵列包含许多具有不同特性的元件的其中之一是所谓indexdata
$一个子阵列b$ b
该数组的每个元素都具有多个属性,其中一个是所谓的数据字段另一个数组。这有很多键值对的属性。
因此,在本质上我有3个独立阵列的层次结构。
我希望得到的层次结构的第一层+第二级,其中3级项目不匹配的所有元素,即排除只从第2级的项目,其中3级项目比赛。
我试图接近这个许多不同的方式,但到目前为止,我没有得到任何地方,任何人都可以帮忙。
仅供参考 - 这里是我的最新尝试。
变种q = idxListResponse.Index.Where(A =>
a.IndexData.All(b = GT;
b.DataField.All(C =>
_index.Index.Where(Z =>
z.IndexData.All(Y = GT; y.DataField.Contains(c.name))
)
)
)
);
的 除了
是这样做的一个很好的方式:
VAR项目= source1.Except(源2);
将返回所有项目在来源1
除了的那些源2
。
由于您的收藏似乎是不同的类型,你会怎么做是这样的:
source1.Except(source2.Select(S = GT; / *选择这里* /))
或者你可以创建自己的实现的IEqualityComparer
和使用该比较两种不同类型的
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))
)
)
)
);
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.
这篇关于C#LINQ - 获得数组元素不以不同的阵列存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!