【发布时间】:2019-11-05 18:25:31
【问题描述】:
我正在尝试将 2 个集合与字符串列表进行比较。 第一个带有字符串列表的对象:
a0, b0, c0
a1, b1, c0
a1, b2, c0
a1, b2, c1
第二次
a0, b0, c0
a1, b2, c0
我有方法去从对象中获取字符串列表
public List<string> GetPlainRow(int index)
{
if(index >= _countRows || index < 0)
{
throw new IndexOutOfRangeException();
}
List<string> returnedRow = new List<string>();
foreach (KeyValuePair<string, List<string>> entry in _dataRows)
{
returnedRow.Add(entry.Value[index]);
}
return returnedRow;
}
试图将对象列表与:
for (int i = 0; i < dataCollection.CountRows; i++)
{
var newRow = table.NewRow();
for (var j = 0; j < dataCollection.GetPlainRow(i).Count; j++)
{
newRow[j] = dataCollection.GetPlainRow(i)[j];
}
for (int k = 0; k < dataCollection.CountRows; k++)
{
for (int l = 0; l < dataRestrained.CountRows; l++)
{
if(dataCollection.GetPlainRow(k).SequenceEqual(dataRestrained.GetPlainRow(l)))
{
found = true;
}
}
}
newRow[dataCollection.CountRows - 2] = found;
found = false;
table.Rows.Add(newRow);
}
但似乎每次它都返回 true,但是当我使用 Equals 而不是 SequenceEqual 时它返回 false。
【问题讨论】:
-
花点时间阅读帮助中心的editing help。 Stack Overflow 上的格式化与其他站点上的不同。您的帖子看起来越好,其他人就越容易阅读和理解它。
-
Equals对于两个列表,仅当两个列表变量引用相同的实例时才会返回 true,而不是它们包含相同的数据。SequenceEqual实际上会检查列表中的数据。 -
我无法理解所需的输出。如果输入是上述两个
List<List<string>>变量,那么输出是什么? -
在这种情况下,“比较”是什么意思?
标签: c#