【问题标题】:How to compare two lists of lists of strings如何比较两个字符串列表列表
【发布时间】: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&lt;List&lt;string&gt;&gt;变量,那么输出是什么?
  • 在这种情况下,“比较”是什么意思?

标签: c#


【解决方案1】:

为了简单起见,我为结果添加了一个类:

   internal class Result
            {
                public List<string> List { get; set; }
                public bool IsExisted { get; set; }
            }

然后我做了一个 EqualityComparer ....

  internal class ListOfStringEqualityComparer : IEqualityComparer<List<string>>
        {
            public bool Equals(List<string> b1, List<string> b2)
            {
                if (b1.Count != b2.Count) return false;
                for (int i = 0; i < b1.Count; i++)
                {
                    if (b1[i] != b2[i]) return false;
                }
                return true;
            }

            public int GetHashCode(List<string> b2)
            {
                int hCode = 0;
                for (int i = 0; i < b2.Count; i++)
                {
                    hCode = EqualityComparer<string>.Default.GetHashCode(b2[i]);
                }
                return hCode.GetHashCode();
            }
        }

现在你可以试试这个:

 ListOfStringEqualityComparer  listOfStringEqualityComparer= new ListOfStringEqualityComparer();
        var q = (from c in bigList
                 select new Result() { List = new List<string>(c), IsExisted = smallList.Contains(c, listOfStringEqualityComparer) });

如果有帮助,现在让我...

【讨论】:

  • 忘记字符串列表的列表。让我们把它当作字符串列表。我有几个字符串列表,假设它们是'A',我有几个字符串列表 - 'B'。 'A' 包含 'B' 中的所有列表。 DataGrid 基于带有真/假的额外列的“A”。我想将 A 与 B 进行比较。如果当前正在比较的“A”元素在“B”列中,则此列表将为真,如果不在“B”列中,则为假
【解决方案2】:

可能是这样的

class Someclass
{
  string data;
  bool found;
}

Someclass data = A.Select(c => new Someclass(){data = c, found = B.Contains(c)});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-07
    • 2014-02-21
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多