我有一个包含一些ID的List。我想从DataTable中删除行= ListLinkedIds
List<string> ListLinkedIds = new List<string>(); //This has values such as 6, 8, etc.
DataSet ds = new DataSet();
SqlDataAdapter da = null;
DataTable dt = new DataTable();
da = new SqlDataAdapter("SELECT TicketID, DisplayNum, TranstypeDesc, SubQueueId, EstimatedTransTime,LinkedTicketId FROM vwQueueData WHERE (DATEADD(day, DATEDIFF(day, 0, Issued), 0) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) AND QueueId = @QueueId AND SubQueueId = @SubQueueId AND LinkedTicketId != @LinkedTicketId AND Called IS NULL", cs);
da.SelectCommand.Parameters.AddWithValue("@QueueId", Queue);
da.SelectCommand.Parameters.AddWithValue("@SubQueueId", SubQueue);
da.SelectCommand.Parameters.AddWithValue("@LinkedTicketId", ListLinkedIds[x]);
da.Fill(ds);
//Removes from DataTable
for (int x = 0; x < ListLinkedIds.Count(); x++)
{
//Remove Row from DataTable Where ListLinkedIds[x]
}
gvMain.DataSource = ds;
gvMain.DataBind();
我尝试了dt.Rows.RemoveAt(删除),但只删除了行号。我想删除ListLinkedIds中的每一行。
答案 0 :(得分:10)
使用LINQ,您可以创建一个新的DataTable
,如:
DataTable newDataTable = dt.AsEnumerable()
.Where(r=> !ListLinkedIds.Contains(r.Field<string>("IDCOLUMN")))
.CopyToDataTable();
答案 1 :(得分:2)
您可以选择行,然后删除返回的结果。
public void test() {
List<string> ListLinkedIds = new List<string>(); //This has values such as 6, 8, etc.
DataSet ds = new DataSet();
SqlDataAdapter da = null;
DataTable dt = new DataTable();
//Removes from DataTable
for (int x = 0; x < ListLinkedIds.Count(); x++)
{
DataRow[] matches = dt.Select("ID='" + ListLinkedIds[x] + "'");
foreach (DataRow row in matches) {
dt.Rows.Remove(row);
}
}
}
答案 2 :(得分:0)
填充数据表后尝试Delete。
for (int x = 0; x < ListLinkedIds.Count(); x++)
{
foreach (DataRow dr in dt.rows)
{
if(dr["id"] == ListLinkedIds[x])
dr.Delete();
}
dt.AcceptChanges();
}