开发者

Remove elements from array A if those elements are contained in arrays B0 ... Bn - using Linq?

I have a character array char[] a_chA that contains elements i don't want.

The elements i don't want are in some more character arrays in a list List<char[]> l_a_chB.

I would like to remove all the unwanted elements using linq, but i can't seem to get the syntax right!

Something like ...

char[] a_chResult = l_a_chB.All(chRemove => a_chA开发者_如何学C.Union(ch => ch != chRemove))

Any help is much appreciated.


var charsInAExceptInAllBs = a_chA.Where(a => !l_a_chB.SelectMany(x => x).Contains(a)).ToArray();

This takes the elements in the B lists, flattens them (that's the SelectMany call) and then filters the elements of A by those characters that apper in the newly flattened B lists (that's the Where call.)

Note that if you don't have any duplicates in A (or don't care about losing duplicates) then you can use this:

var charsInAExceptInAllBs = a_chA.Except(l_a_chB.SelectMany(x => x)).ToArray();

Because Except takes the set difference, it will eliminate duplicates in the final result.


var chars = from c in l_a_chB
            from x in c
            select x;    
var result = a_chA.Except(chars);

is what you are looking for.


var result = a_chA.Except(l_a_chB.SelectMany(a=>a));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜