string[] strPname = new string[]{A,B}
怎么先把strTPname 中的重复项去掉,然后再排除掉strPname 中的项,输出一个新数组?
结果是{E,D,C}
先谢谢了
6 个解决方案
#1
如果用循环写代码就多了,还是用 LINQ吧:
string[] strTPname = new string[]{"A","A","B","C"};
string[] strPname = new string[] { "B" };
string[] result = strTPname.Distinct().Except(strPname).ToArray();
string[] strTPname = new string[]{"A","A","B","C"};
string[] strPname = new string[] { "B" };
string[] result = strTPname.Distinct().Except(strPname).ToArray();
#2
额,虽然不懂linq,也没用过,但是试了一下,你这句话,真心好用!!!
![C# 数组去重复,并且减去另一个数组。 C# 数组去重复,并且减去另一个数组。](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9hSFIwY0hNNkx5OW1iM0oxYlM1amMyUnVMbTVsZEM5UWIybHVkRVp2Y25WdEwzVnBMM05qY21sd2RITXZZM05rYmk5UWJIVm5hVzR2TURBekwyMXZibXRsZVM4eUxtZHBaZz09.jpg?w=700&webp=1)
#3
+1
#4
Linq在集合操作中的优越性是显而易见的。
#5
linq 正解 ,非常方便
#6
#1
如果用循环写代码就多了,还是用 LINQ吧:
string[] strTPname = new string[]{"A","A","B","C"};
string[] strPname = new string[] { "B" };
string[] result = strTPname.Distinct().Except(strPname).ToArray();
string[] strTPname = new string[]{"A","A","B","C"};
string[] strPname = new string[] { "B" };
string[] result = strTPname.Distinct().Except(strPname).ToArray();
#2
额,虽然不懂linq,也没用过,但是试了一下,你这句话,真心好用!!!
![C# 数组去重复,并且减去另一个数组。 C# 数组去重复,并且减去另一个数组。](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9hSFIwY0hNNkx5OW1iM0oxYlM1amMyUnVMbTVsZEM5UWIybHVkRVp2Y25WdEwzVnBMM05qY21sd2RITXZZM05rYmk5UWJIVm5hVzR2TURBekwyMXZibXRsZVM4eUxtZHBaZz09.jpg?w=700&webp=1)
#3
+1
#4
Linq在集合操作中的优越性是显而易见的。
#5
linq 正解 ,非常方便