-4

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

Id  Value
1   football
2   Tennis
3   Cricket

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

4

2 回答 2

0

我猜那是数据集中的数据表。首先需要查询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);
}

希望能帮助到你

于 2012-09-28T04:39:29.337 回答
0

首先,您应该使用循环来查看您的数据集列“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);
于 2012-09-28T04:56:27.667 回答