[英]How to check if a Row is already in a DataTable?
我创建了一个DataTable
如下
DataTable dt2 = new DataTable();
DataColumn dc = new DataColumn();
dc.ColumnName = "Table Allocated";
dt2.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Capacity";
dt2.Columns.Add(dc);
向其中添加了一些行,稍后又创建了另一行
DataRow resRow;
resRow = dt2.NewRow();
resRow["Table Allocated"] = "1"
resRow["Capacity"] = "xx";
如何以最佳方式检查DataTable
dt2 aginist所有列中是否已存在resRow?
你可以试试这个
var results = from myRow in dt2.AsEnumerable()
where myRow.Field<string>("Table Allocated") == resRow["TableAllocated"]
&& myRow.Field<string>("Capacity")==resRow["Capacity"]
select myRow;
如果result.count()> 0,则存在一行。
您可以尝试如下操作:
var rowPresent = dt2.Select("Table Allocated=" + resRow["TableAllocated"] + " AND Capacity = '" + resRow["Capacity"] + "'");
现在,您可以检查rowPresent
包含任何值,如果没有,则将新行添加到DataTable
一种解决方案是使用RowFilter:
var dataView = new DataView(dt2);
dataView.RowFilter = string.Format("TableAllocated='{0}', tableAllocatedValue);
另一个解决方案是使用Select
dt.Select(string.Format("TableAllocated='{0}', tableAllocatedValue));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.