c# - 尝试比较两个列表 c# - 应该工作吗?

标签 c# list compare sequence

我有一个看起来像这样的方法:

GetDrawing(Dictionary<string, List<string>> AllDrawings, Dictionary<string, bool> ImportData, string[] ItemsToCompare)

首先,该方法创建一个新的 List<string>所有项目来自 ImportData<string, bool>具有值 true可以在 string[] ItemsToCompare 中找到

其次,我想比较一下新List<string>来自 AllDrawings<string, List<string>> 的列表.该方法最终应该返回一个字符串,其中包含来自 AllDrawings<string>, List<String>> 的键。两个列表匹配的地方。

我现在花了很多时间试图自己解决这个问题,并且还尝试了我可以在 Stackoverflow 上找到类似问题的所有答案,但没有成功。

下面是我的方法的完整代码。如上所述,我尝试了很多不同的方法来比较列表,但下面的方法是最新的尝试。

  public static string GetDrawing(Dictionary<string, List<string>> AllDrawings, Dictionary<string, bool> ImportData, string[] ItemsToCompare)
    {
        string FinalDrawing = "";
        try
        {
            List<string> AllCorrect = new List<string>();
            foreach (var item in ImportData)
            {
                if (item.Value == true && ItemsToCompare.Contains(item.Key))
                    AllCorrect.Add(item.Key);
            }

            AllCorrect.Sort();

            foreach (var DrawItem in AllDrawings)
            {

                DrawItem.Value.Sort();
                var match = AllCorrect.SequenceEqual(DrawItem.Value);

                if (match == true)
                {
                    FinalDrawing = DrawItem.Key;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return FinalDrawing;
    }

我的问题是 var match = AllCorrect.SequenceEqual(DrawItem.Value); 的返回值是false因此 FinalDrawing永远不会设置。

非常感谢所有答案。 提前致谢!

最佳答案

好的..我已经在评论中说过了,但只是为了确保你不会因为不使用 linq 等而被大吼大叫:

就您告诉我们的而言,您的程序似乎是正确的。

这是一个简单的测试。我提供了一些 stub 数据,涵盖了您似乎要检查的所有内容:

  • 只有 true 来自 importeddata 的东西
  • 仅在 itemstocompare 中列出的事物
  • 输入列表未排序

-> http://rextester.com/HAE73942

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

public class Program
{
    // your original function, nothing changed
    public static string GetDrawing(Dictionary<string, List<string>> AllDrawings, Dictionary<string, bool> ImportData, string[] ItemsToCompare)
    {
        string FinalDrawing = "";
        try
        {
            List<string> AllCorrect = new List<string>();
            foreach (var item in ImportData)
            {
                if (item.Value == true && ItemsToCompare.Contains(item.Key))
                    AllCorrect.Add(item.Key);
            }

            AllCorrect.Sort();

            foreach (var DrawItem in AllDrawings)
            {

                DrawItem.Value.Sort();
                var match = AllCorrect.SequenceEqual(DrawItem.Value);

                if (match == true)
                {
                    FinalDrawing = DrawItem.Key;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return FinalDrawing;
    }

        public static void Main(string[] args)
        {
            var allDrawings = new Dictionary<string, List<string>>();
            allDrawings.Add("aaa", new List<string>{ "a03", "a01", "a02" }); // originally unsorted
            allDrawings.Add("bbb", new List<string>{ "b03", "b01", "b02" }); // originally unsorted
            allDrawings.Add("ccc", new List<string>{ "c03", "c01", "c02" }); // originally unsorted

            var import = new Dictionary<string, bool>();
            import.Add("b01", false); // falsey
            import.Add("a05", true); // not in comparison
            import.Add("a03", true);
            import.Add("c01", false); // falsey
            import.Add("a02", true);
            import.Add("a04", true); // not in comparison
            import.Add("a01", true);

            var toCompare = new string[9];
            toCompare[0]="a01"; toCompare[1]="a02"; toCompare[2]="a03";
            toCompare[3]="b01"; toCompare[4]="b02"; toCompare[5]="b03";
            toCompare[6]="c01"; toCompare[7]="c02"; toCompare[8]="c03";

            var result = GetDrawing(allDrawings, import, toCompare);

            Console.WriteLine("Result: " + result);
        }
}

它工作正常并按预期打印 aaa

这意味着您必须忽略输入数据中的某些内容。也许有些字符串是大写/小写的?也许有些字符串内部有空格,而有些则没有?

关于c# - 尝试比较两个列表 c# - 应该工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32473803/

相关文章:

Python - 将列表数据类型输出为 CNF

python - 如何将列表分组为包含顺序的 n 元组

c - 如何比较C中链表中的每个项目?

java - 寻找 Java 的最大值(value)

java - Java 比较构造函数对象

c# - List<> 集合成员在不应该更改时更改

c# - XML 解析 : line 1, 字符 7,需要分号

c# - 在 Azure 上运行重复任务

list - 使用斐波那契数列创建无限列表

c# WPF 如何在不声明新源的情况下从 mediended 事件处理程序重复 MediaElement 播放?