如果是,那是什么?
编辑:回应以下评论:
var tabulatedOutputErrors = from error in outputErrors
group error by error into errorGroup
select new { error = errorGroup.Key, number = errorGroup.Count() };
var tabulatedInputErrors = from error in inputErrors
group error by error into errorGroup
select new { error = errorGroup.Key, number = errorGroup.Count() };
var problems = tabulatedOutputErrors.Except(tabulatedInputErrors);
如果需要,您可以扩展计数。
最佳答案
LINQ 有 Enumerable.Except
扩展方法,这似乎是您要找的。p>
例子:
var list1 = new int[] {1, 3, 5, 7, 9};
var list2 = new int[] {1, 1, 5, 5, 5, 9};
var result = list1.Except(list2); // result = {3, 7}
替代方案:
从 .NET 3.5 开始,还存在 HashSet<T>
类(以及 .NET 4.0 中类似的 SortedSet<T>
类。此类(或者说 .NET 4.0 中的 ISet<T>
接口(interface))有一个 ExceptWith
方法也可以完成这项工作。
例子:
var set1 = new HashSet<int>() {1, 3, 5, 7, 9};
var set2 = new HashSet<int>() {1, 1, 5, 5, 5, 9};
set1.ExceptWith(set2); // set1 = {3, 7}
当然,这种方法是否更可取取决于上下文/用法。在大多数情况下,效率优势(就地执行差异操作并使用哈希码)可能可以忽略不计。无论哪种方式,请选择。 :)
关于c# - 是否有与 C++ 的 std::set_difference 等效的 C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3196341/