首页 > 解决方案 > 在 2 个数组 c# 中查找匹配值

问题描述

所以我试图在文件名和 SQL 数据集之间进行匹配。

例如 Recordset 包含一个 1000,1001,1002,1003 的数组

我的文件列表包含一个数组 "c:\temp\100.txt","c:\temp\1000.txt","c:\temp\1001.txt","c:\temp\2002.txt" ,"c:\temp\100.txt","c:\temp\1000-1.txt"

所以我的目标是得到以下结果:

c:\temp\1000.txt c:\temp\1001.txt c:\temp\1000-1.txt

我如何做到这一点?

我试过这样的东西

     private static String[] GetImageList()
    {
        return Directory.GetFiles("C:\\tempbilledeimport\\", "*.*", SearchOption.AllDirectories);

    }

    private static List<string> SQLRecordset()
    {
        List<string> items = new List<string>();

        string queryString = "select item from inventory where WebUseOnWeb=1";
        using (SqlConnection connection = new SqlConnection(DatabaseConnection.SqlConnectionStringCompany))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            // Call Read before accessing data.
            items.Add(reader[0].ToString());
            reader.Close();
        }

        return items;
    }

但是我如何比较它们呢?

标签: c#

解决方案


创建一个包含文件名的整数 HashSet,您可以从其中将 c:\temp\ 和 .txt 替换为空字符串,然后将它们转换为整数。

您现在可以检查您获得的每个整数是否在 HashSet 中都有匹配项。

我可以看到你有一个 1000-1.txt 文件。不知道你想如何处理。是比赛吗?


推荐阅读