【问题标题】:Find a vaule from columns of a dataset asp.net从数据集 asp.net 的列中查找值
【发布时间】:2012-09-19 23:02:12
【问题描述】:

我想从数据集列 ID 中查找值。 这是数据集

Id  Value
1   football
2   Tennis
3   Cricket

如果列中不存在任何一个,那么我想在数据集中附加该特定值

【问题讨论】:

  • 列中不存在是什么意思?
  • 如果 id = 1 或 2 或 3 不存在,那么我需要使用数据集添加特定行

标签: asp.net dataset


【解决方案1】:

首先,您应该使用循环来查看您的数据集列“id”是否包含该值。如果 id 不存在,则:

   DataRow newrow = ds.Tables[0].NewRow(); //assuming ds is your dataset
   newrow["id"] = "your new id value";
   newrow["value"] = "your new value";
   ds.Tables[0].Rows.Add(newrow);

【讨论】:

    【解决方案2】:

    我猜这是DataSet 中的DataTable。首先需要查询id是否在DataTable中:

    var dataTable = dataSet.Tables[0]; //For this example I'm just getting the first DataTable of the DataSet, but it could be other.
    var id = 1;
    var value = "football";
    
    //Any(...) will return true if any record matches the expression. In this case, the expression is if a Id Field of the row is equals to the provided id
    var contained = dataTable.AsEnumerable().Any(x =>x.Field<int>("Id") == id);
    

    然后,如果它不存在,则添加一个新行:

    if(!contained)
    {
        var row = dataTable.NewRow();
    
        row["Id"] = id;
        row["Value"] = value;
    
        dataTable.Rows.Add(row);
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 2011-10-27
      • 2015-10-24
      • 1970-01-01
      • 2020-07-18
      相关资源
      最近更新 更多