是否有一种相对简单的方法来获取.NET中两个DataTable的交集?
我可以想到显而易见的方法(在O(n ^ 2)中自己迭代两个表),但如果它可用,我想要一些更优雅的东西.我怀疑可能有一种我没有看到的聪明方式.当然,可读性和可维护性很重要,所以我试图远离任何太"光滑"的东西.
有什么好主意吗?
编辑:看起来布莱恩沃茨有一个非常好的3.5解决方案,但不幸的是我在.NET 2.0(我应该提到.)
Bry*_*tts 12
使用.NET 3.5:
using System.Data;
public static class DataTableExtensions
{
public static IEnumerable<DataRow> Intersect(this DataTable table, DataTable other)
{
return table.AsEnumerable().Intersect(other.AsEnumerable());
}
public static IEnumerable<DataRow> Intersect(this DataTable table, DataTable other, IEqualityComparer<DataRow> comparer)
{
return table.AsEnumerable().Intersect(other.AsEnumerable(), comparer);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12900 次 |
| 最近记录: |