您如何获取两个数据表并匹配值,然后在匹配的行值中的列中添加一些内容



我有两个数据表,我需要匹配每个数据表中的邮政编码,每次程序找到匹配项时,我都需要将匹配行中的一列设置为字符串。这是我的代码。

我不知道在foreach循环中该怎么做,或者如果有更简单的方法可以做到这一点,请说出来。

        //Datatable for entire list of zipcodes
        DataTable datat = new DataTable("DATA");
        DataColumn state = new DataColumn();
        DataColumn county = new DataColumn();
        DataColumn zipcode = new DataColumn();
        DataColumn latitude = new DataColumn();
        DataColumn longitude = new DataColumn();
        DataColumn salesperson = new DataColumn();
        DataColumn originalrec = new DataColumn();
        //Add the columns to the datatable
        datat.Columns.Add(state);
        datat.Columns.Add(county);
        datat.Columns.Add(zipcode);
        datat.Columns.Add(latitude);
        datat.Columns.Add(longitude);
        datat.Columns.Add(salesperson);
        datat.Columns.Add(originalrec);
        return datat;
    }
    private static DataTable InitData2()
    {
        //Datatable for entire list of zipcodes
        DataTable datat2 = new DataTable("DATA");
        DataColumn ctype = new DataColumn();
        DataColumn csalesperson = new DataColumn();
        DataColumn czipcode = new DataColumn();
        //Add the columns to the datatable
        datat2.Columns.Add(ctype);
        datat2.Columns.Add(csalesperson);
        datat2.Columns.Add(czipcode);
        return datat2;
    }       
    private static String InitPath()
    {
        string path = "C:/Documents and Settings/Andre/Desktop/SalesMap/data.csv";
        return path;
    }
    private static String InitPath2()
    {
        string path2 = "C:/Documents and Settings/Andre/Desktop/SalesMap/CUSTOMER_PROSPECT_SALESPERSON.csv";
        return path2;
    }
    public static void Main()
    {
        try
        {
            DataTable dt = InitData1();
            DataTable dt2 = InitData2();
            StreamReader sr = new StreamReader(InitPath());
            StreamReader sr2 = new StreamReader(InitPath2());
            String csvData = string.Empty;
            String csvData2 = string.Empty;
            while ((csvData = sr.ReadLine()) != null)
            {
                String[] data = csvData.Split(',');
                //dt.Rows.Add(data);
                DataRow newRow1 = dt.NewRow();
                newRow1[0] = data[0].ToString();
                newRow1[1] = data[1].ToString();
                newRow1[2] = data[2].ToString();
                newRow1[3] = data[3].ToString();
                newRow1[4] = data[4].ToString();
                newRow1[5] = "";
                newRow1[6] = "";
                dt.Rows.Add(newRow1);
                Console.WriteLine("Row added for: dt");
            }
            dt.WriteXml(@"c:testdt1.xml");
            sr.Close();
            while ((csvData2 = sr2.ReadLine()) != null)
            {
                String[] data2 = csvData2.Split(',');
                DataRow newRow2 = dt2.NewRow();
                newRow2[0] = data2[0].ToString();
                newRow2[1] = data2[1].ToString();
                newRow2[2] = data2[2].ToString();
                dt2.Rows.Add(newRow2);
                Console.WriteLine("Row added for: dt2");
            }
            dt2.WriteXml(@"c:testdt2.xml");
            sr2.Close();
            foreach (DataRow row1 in dt.Rows)
            {
                //Dont know what to do here
            }
        }
        catch (Exception e)
        {
            //Console.WriteLine("The files could not be read:");
            //Console.WriteLine(e.Message);
            StreamWriter sw = new StreamWriter(@"c:testerror.txt");
            sw.WriteLine(e.Message);
            sw.Close();

        }
    }
}
}

不确定我是否完全理解。但正如我可以想象的那样,它的外观应该是这样的:

            DataTable dtSampleOne = new DataTable();
            DataTable dtSampleTwo = new DataTable();
            foreach (DataRow rowSampleOne in dtSampleOne.Rows)
            {
                string zipCodeSampleOne = rowSampleOne["zipCodeToMatchOne"].ToString();
                foreach (DataRow rowSampleTwo in dtSampleTwo.Rows)
                {
                    if (rowSampleTwo["zipCodeToMatchTwo"].ToString().Equals(zipCodeSampleOne))
                    {
                        // Do your stuff here
                    }                            
                }
            }

如果您需要什么,我会保持联系。

最简单的方法是在存储过程中完成所有操作。因此,存储过程执行简单匹配(可能使用相交)并返回结果。如果您不能使用存储过程,那么我会选择两个集合并加载到 List 中,然后您可以使用 LINQ 命令执行相交。无需每个循环。

List<string> list1 = // load list from db
List<string> list2 = // load second list from db
var result = list1.Intersect(list2);

就是这样

使用 LINQ,我们可以进一步改进 Luis Hernández 给出的代码,尽管它可以完成这项工作。

在给定的解决方案(路易斯·埃尔南德斯)中,我们有 2 个循环。假设样本一为 100 行,样本二为 200 行。我们最终将循环 100 X 200 次!

相反,我们可以获取匹配的记录,并且我们可以仅使用这些记录进行循环访问。这将避免不必要的循环。

        var sampleOne = new DataTable();
        var sampleTwo = new DataTable();
        // TODO : I assume these two table have the common column "ZipCode".
        // TODO : Add your sample data here!
        var matchingRows = from s1 in sampleOne.AsEnumerable()
                           join s2 in sampleTwo.AsEnumerable() on s1.Field<string>("ZipCode") equals s2.Field<string>("ZipCode")
                           select s1;
        foreach (var row in matchingRows)
        {
            // Do your stuff here !
        }

如果我的解决方案不符合标准,请不要犹豫通知我!

相关内容

  • 没有找到相关文章

最新更新