Finding the intersection of two .NET DataTables

前端 未结 4 1532
野的像风
野的像风 2020-12-09 22:30

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

I can think of the obvious ways (iterating over both tables myself in O(

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 22:45

    With .NET 3.5:

    using System.Data;
    
    public static class DataTableExtensions
    {
        public static IEnumerable Intersect(this DataTable table, DataTable other)
        {
            return table.AsEnumerable().Intersect(other.AsEnumerable());
        }
    
        public static IEnumerable Intersect(this DataTable table, DataTable other, IEqualityComparer comparer)
        {
            return table.AsEnumerable().Intersect(other.AsEnumerable(), comparer);
        }
    }
    

提交回复
热议问题