c# - 在 C# 中比较字典中的数组
问题描述
我正在尝试比较字典中存在的数组,如果数组相同则传递键。我正在使用类似这样的语法来检查值,任何人都可以帮助使用正确的语法。
Dictionary<int, string[]> test = excel.GetSheetColumnPairs(xlWorkbook, myint);
//loop dictionary all elements
foreach (KeyValuePair<int, string[]> pair in test)
{
Console.WriteLine(pair.Key + "....." + pair.Value + "<br />");
}
//find dictionary duplicate values.
var duplicateValues = test.GroupBy(x => x.Value).Where(x => x.Count() > 1);
Console.WriteLine("<br /><b>dictionary duplicate values..........</b><br />");
//loop dictionary duplicate values only
foreach (var item in duplicateValues)
{
Console.WriteLine(item.Key + "<br />");
}
解决方案
Assuming that the keys are always different and your code don't explode: The GroupBy is using the equality implementation for arrays. By default two arrays will be equal if they are referencing the same object (the same array); i.e: the default equality implementation won't care about the elements of the array, it only test that the referenced object are the same object. If you want to know if two arrays (different references, different objects in memory) contains the same elements, you have to create your own method and compare the elements one by one(or you can override the equality implementation)
推荐阅读
- c# - ASP.NET Core Identity GenerateTwoFactorTokenAsync 返回空字符串
- php - 如何在 Drupal 8 中为子菜单设置自定义类?
- c# - 包与 netcoreapp2.2 不兼容
- javascript - Javascript:确定参数是否传递给函数
- r - R - colMeans(wind.speed,na.rm = T)中的错误:'x'必须是数字
- angular - spinner inside button has incorrect color
- xml - ASP.NET Core 的 XML 配置提供程序中的数组
- vba - 根据下拉值选择更改内容控件属性文本样式
- c++ - 模型类无法在 DirectX 10 中初始化
- typescript - 在 Object.assign 上出现 TypeScript 编译错误,但一切正常