gpt4 book ai didi

c# - 枚举 DataTable,过滤项目,然后还原为 DataTable

转载 作者:行者123 更新时间:2023-11-30 17:13:48 25 4
gpt4 key购买 nike

我想过滤我的 DataTable 中的项目通过将列值转换为 IEnumerable<DataRow> 来确定列值是否包含在字符串数组中,之后我想将其重新转换为 DataTable因为这是我的方法必须返回的内容。

到目前为止,这是我的代码:

string[] ids = /*Gets string array of IDs here*/
DataTable dt = /*Databasecall returning a DataTable here*/
IEnumerable<DataRow> ie = dt.AsEnumerable();
ie = ie.Where<DataRow>(row => ids.Contains(row["id"].ToString()));
/*At this point I've filtered out the entries I don't want, now how do I convert this back to a DataTable? The following does NOT work.*/
ie.CopyToDataTable(dt, System.Data.LoadOption.PreserveChanges);
return dt;

最佳答案

我将创建数据表的空克隆:

DataTable newTable = dt.Clone();

然后从旧表中导入与过滤器匹配的行:

foreach(DataRow row in ie)
{
newTable.ImportRow(row);
}

关于c# - 枚举 DataTable,过滤项目,然后还原为 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9468071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com