循环遍历两个数据表以匹配值 [英] Looping through two datatables to match a value

查看:147
本文介绍了循环遍历两个数据表以匹配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遍历两个不同的数据表,以匹配两个表中一行中的给定值。



下面:



I am trying to loop through two different datatables to match a given value in a row on both tables.

Below:

foreach(DataRow dr in dtAccounts.Rows)
            {
                string customerIDSample1 = dr["custID"].ToString();                 

                foreach(DataRow dr2 in dtCustomer.Rows)
                {
                    if (dr2["custID"].ToString().Equals(customerIDSample1))
                    {
                         
                        lblCurrentPointsAmount.Text = "";
                    }
                }
            }

        }





上面的代码似乎正在正确地检查行,但是如何从包含匹配值的行的dtAccounts获取行号?



这是我知道行号所以我可以从dtAccounts中与custID匹配在同一行的另一列中提取一段数据值。





我很挣扎所以任何帮助都非常感谢:)



亲切的问候,



格伦。



The code above seems to be checking the rows correctly, but how can I get the row number from the dtAccounts for the row that contains the matching value?

This is so I know the row number so I can pull a piece of data from another column in the dtAccounts that is on the same row as the "custID" matching value.


I'm struggling so any help is much appreciated :)

Kind Regards,

Glen.

推荐答案

你可以这样做,只需更换 foreach 循环使用相当于一个:



You can do that simply replacing the foreach loop with the equivalent for one:

for(int r = 0; r < dtAccounts.Rows.Count; ++r)
{
  DataRow dr = dtAccounts.Row[r];
  // same as above
  // ..


Hi
试试这样。



Hi Try like this.

bool flag = false;
       foreach (DataRow dr in dtAccounts.Rows)
       {
           string customerIDSample1 = dr["custID"].ToString();

           foreach (DataRow dr2 in dtCustomer.Rows)
           {
               if (dr2["custID"].ToString().Equals(customerIDSample1))
               {

                   lblCurrentPointsAmount.Text = "";
                   flag = true; break;
               }
           }
           if (flag)
               break;
       }







(或)










(or)



var Accounts = dtAccounts.Rows.OfType<DataRow>().Select(k => k["custID"] + "").ToList();
       var customers = dtCustomer.Rows.OfType<DataRow>().Select(k => k["custID"] + "").ToList();
       bool matches = Accounts.Any(k => customers.Contains(k));
       if (matches)
       {
          // lblCurrentPointsAmount.Text = "";
       }


这篇关于循环遍历两个数据表以匹配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆