【问题标题】:Find row in datatable with specific id在具有特定 ID 的数据表中查找行
【发布时间】:2014-01-05 11:06:38
【问题描述】:

我在数据表中有两列:

ID, Calls. 

如何找到 Calls 的值是where ID = 5

5 可以是任意数字,仅作为示例。每行都有一个唯一的 ID。

【问题讨论】:

  • 您不能在选择语句中执行此操作吗? “选择 ID,来自 MyTable 的调用,其中 ID=@id_search”。然后只需向数据库调用提供“@id_search”参数。这将比 LINQ 更快,尤其是假设 ID 是主键或索引。
  • 恐怕不是数据库,数据集/数据表。

标签: c# datatable datarow


【解决方案1】:

创建一个要搜索的字符串条件,如下所示:

string searchExpression = "ID = 5"

然后使用DataTable对象的.Select()方法,像这样:

DataRow[] foundRows = YourDataTable.Select(searchExpression);

现在您可以循环遍历结果,如下所示:

int numberOfCalls;
bool result;
foreach(DataRow dr in foundRows)
{
    // Get value of Calls here
    result = Int32.TryParse(dr["Calls"], out numberOfCalls);

    // Optionally, you can check the result of the attempted try parse here
    // and do something if you wish
    if(result)
    {
        // Try parse to 32-bit integer worked

    }
    else
    {
        // Try parse to 32-bit integer failed

    }
}

【讨论】:

  • 这确实给了我数据行,但是我如何从列调用中获取特定值
  • @RyanMurphy - 更新了答案以显示如何从行中获取值并安全地尝试解析值。
  • @RyanMurphy - 没问题,如果您还没有投票,也可以随意投票。 :-)
  • 如果我想获取结果的行号怎么办。我怎样才能得到它?
【解决方案2】:

您可以使用 LINQ to DataSet/DataTable

var rows = dt.AsEnumerable()
               .Where(r=> r.Field<int>("ID") == 5);

由于每一行都有一个唯一的 ID,您应该使用Single/SingleOrDefault,如果您返回多条记录,它将引发异常。

DataRow dr = dt.AsEnumerable()
               .SingleOrDefault(r=> r.Field<int>("ID") == 5);

(将 int 替换为您的 ID 字段类型)

【讨论】:

  • 有时无法使用此“DataRow[] foundRows = YourDataTable.Select(searchExpression);”从表中选择行。但是当我使用 LINQ 时。它的工作很好。谢谢哈比布。
【解决方案3】:

你可以试试方法选择

DataRow[] rows = table.Select("ID = 7");

【讨论】:

    【解决方案4】:

    我可以使用以下代码。 谢谢大家。

    int intID = 5;
    DataTable Dt = MyFuctions.GetData();
    Dt.PrimaryKey = new DataColumn[] { Dt.Columns["ID"] };
    DataRow Drw = Dt.Rows.Find(intID);
    if (Drw != null) Dt.Rows.Remove(Drw);
    

    【讨论】:

      【解决方案5】:
      DataRow dataRow = dataTable.AsEnumerable().FirstOrDefault(r => Convert.ToInt32(r["ID"]) == 5);
      if (dataRow != null)
      {
          // code
      }
      

      如果是类型化的DataSet:

      MyDatasetType.MyDataTableRow dataRow = dataSet.MyDataTable.FirstOrDefault(r => r.ID == 5);
      if (dataRow != null)
      {
          // code
      }
      

      【讨论】:

        【解决方案6】:

        试试这个代码

        DataRow foundRow = FinalDt.Rows.Find(Value);
        

        但至少设置一个主键

        【讨论】:

        【解决方案7】:

        您好,只需创建一个如下所示的简单函数。它返回输入的调用参数有效或为真的所有行。

         public  DataTable SearchRecords(string Col1, DataTable RecordDT_, int KeyWORD)
            {
                TempTable = RecordDT_;
                DataView DV = new DataView(TempTable);
                DV.RowFilter = string.Format(string.Format("Convert({0},'System.String')",Col1) + " LIKE '{0}'", KeyWORD);
                return DV.ToTable();
            }
        

        并简单地调用它,如下所示;

          DataTable RowsFound=SearchRecords("IdColumn", OriginalTable,5);
        

        其中 5 是 ID。 谢谢。。

        【讨论】:

          【解决方案8】:

          尽量避免不必要的循环,如果需要就这样做。

          string SearchByColumn = "ColumnName=" + value;
          DataRow[] hasRows = currentDataTable.Select(SearchByColumn);
          if (hasRows.Length == 0)
          {
              //your logic goes here
          }
          else
          {
              //your logic goes here
          }
          

          如果您想按特定 ID 进行搜索,那么表中应该有一个主键。

          【讨论】:

            猜你喜欢
            • 2018-02-02
            • 1970-01-01
            • 2011-08-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-28
            • 1970-01-01
            相关资源
            最近更新 更多