简体   繁体   English

匹配列表集合中的值和C#中DataTable行中的值

[英]Matching values in a list collection and values in the DataTable row in C#

Good day Coders 美好的一天编码员

I would like to know is there a way of matching values from a list collection to a data table row. 我想知道是否有一种方法可以将列表集合中的值与数据表行进行匹配。 Currently I have this, that doesn't work. 目前我有这个,这行不通。 Ref_Number is the list collection and dtRefNum is the DataTable row Ref_Number是列表集合,而dtRefNumDataTable

 int count = Ref_Number.Count > dtRefNum.Rows.Count ? dtRefNum.Rows.Count : Ref_Number.Count;
 for (int i = 0; i < count; i++ )
 {
     if (Ref_Number[i].ToString().Trim().Contains(dtRefNum.Rows[i].ToString().Trim()))
      {
           var refnum = Ref_Number[i].ToString().Trim();
           var fsdfsdf = dtRefNum.ToString().Trim();
      }
      else if (Ref_Number[i].ToString().Trim() == dtRefNum.Rows[i].ToString().Trim())
      {

      }

You may want to try this, 您可能想尝试一下

//assumming Ref_Number is List<string>
    for (int i = 0; i < dtRefNum.Rows.Count; i++ )
     {
         if ((Ref_Number.Where(rn => rn.ToLower().Trim() == dtRefNum.Rows[i].ToString().ToLower().Trim()).Count()) > 0)
          {
               var refnum = Ref_Number[i].ToString().Trim();
               var fsdfsdf = dtRefNum.ToString().Trim();
          }
    }

I don't know what are you looking for but if you have more rows in the list than in the table, it's ok? 我不知道您要寻找什么,但是如果列表中的行多于表中的行,那可以吗? I don't know. 我不知道。

If you want to use linq, but I think it's the same: 如果要使用linq,但我认为是相同的:

int i=0;
Ref_Number.foreach(oh => 
    {
        if (oh[i].ToString().Trim() = dtrefnum[i].ToString().Trim(); //or use contains whateveryouwant
            ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM