找出两个.NET数据表的交点 [英] Finding the intersection of two .NET DataTables

查看:96
本文介绍了找出两个.NET数据表的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个相对简单的方式来获得的两个DataTable的交集在.NET?

Is there a relatively straightforward way to get the intersection of two DataTables in .NET?

我能想到的明显的方式(遍历两个表自己在为O(n ^ 2)),但我想的东西多一点优雅的,如果它是可用的。我怀疑有可能是我没有看到一种智能的方式。可读性和可维护性是重要的,当然,所以我想远离任何过于油滑。

I can think of the obvious ways (iterating over both tables myself in O(n^2)), but I'd like something a little more elegant if it's available. I suspect there may be an intelligent way that I'm not seeing. Readability and maintainability are important, of course, so I'm trying to stay away from anything too "slick".

有什么好的想法?

编辑:(我应该提到),它看起来像布赖恩·沃茨具有3.5 pretty的很好的解决方案,但不幸的是我在.NET 2.0

推荐答案

使用.NET 3.5:

With .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);
    }
}

这篇关于找出两个.NET数据表的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆