获取“差异"在 C# 中的两个数组之间? [英] Getting the "diff" between two arrays in C#?

查看:47
本文介绍了获取“差异"在 C# 中的两个数组之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这两个数组:

var array1 = new[] {"A", "B", "C"};
var array2 = new[] {"A", "C", "D"};

我想了解两者之间的差异.我知道我可以只用几行代码来写这个,但我想确保我没有遗漏内置的语言功能或 LINQ 扩展方法.

I would like to get the differences between the two. I know I could write this in just a few lines of code, but I want to make sure I'm not missing a built in language feature or a LINQ extension method.

理想情况下,我最终会得到以下三个结果:

Ideally, I would end up with the following three results:

  • 不在数组 1 中但在数组 2 中的项目(D")
  • 项目不在数组 2 中,但在数组 1 中(B")
  • 两者都包含的项目

提前致谢!

推荐答案

如果您有可用的 LINQ,可以使用 ExceptDistinct.你在问题中要求的集合分别是:

If you've got LINQ available to you, you can use Except and Distinct. The sets you asked for in the question are respectively:

- array2.Except(array1)
- array1.Except(array2)
- array1.Intersect(array2)

这篇关于获取“差异"在 C# 中的两个数组之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆