高效过滤数据表
过滤数据表中数据行的最有效方法是什么?我有一个整数列表,想要检索与列表中的整数匹配的所有行(并最终从中创建一个数据表)。我目前正在使用下面的代码,但速度很慢。我是否缺少更有效的方法?
foreach (var i in integerlist)
{
DataRow dr = (from row in originalDataTable.AsEnumerable()
where row.Field<int>("urlID") == i
select row).FirstOrDefault<DataRow>();
if (dr!= null)
{
newDataTable.Rows.Add(dr);
}
}
What's the most efficient way of filtering DataRows in a DataTable? I have a list of integers and want to retrieve all rows (and eventually create a DataTable from them) which match the integers in the list. I'm currently using the code below, but it's quite slow. Am I missing a more efficient way?
foreach (var i in integerlist)
{
DataRow dr = (from row in originalDataTable.AsEnumerable()
where row.Field<int>("urlID") == i
select row).FirstOrDefault<DataRow>();
if (dr!= null)
{
newDataTable.Rows.Add(dr);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议你尝试反之亦然。
如果数据集中的行数比 int 集合中的整数多,则更有意义。
希望它有帮助:)
I suggest you to try to do vice versa.
It makes even more sense if you have more rows in dataset then integers in your int collection.
Hope it helps :)
嗯...可能我错过了一些东西,但是...
使用 DataView 并应用 RowFilter 吗?
Hm... may be I'm missing something, but...
Woudn't be it easier just use DataView and apply a RowFilter for it ?
您可以尝试进行连接,例如:
这应该为您提供完整的结果集。
如果你需要一个数据表,你可以这样做:
resultSet.CopyToDataTable();
you could try doing a join such as:
that should give you the full result set.
if you need a datatable you could do:
resultSet.CopyToDataTable();
正如@Tigran所说,您可以使用数据视图,请检查 这篇 msdn 文章介绍了如何实现这一目标。
基本上,您使用 DataView 来过滤数据,然后调用 DataView.ToTable 方法来获取新的 DataTable。
As @Tigran says you can use the dataview, check this msdn article on how to accomplish just that.
Basically you use a
DataView
to filter the data and the you call theDataView.ToTable
method to get the new DataTable.