使用 LINQ 比较两个数据表

发布于 2024-11-25 21:43:54 字数 635 浏览 8 评论 0原文

我有两个数据库。它们都使用 GetTable1 \ GetTable2 函数填充到 DataTables 中。

我想要做的基本上是使用 LINQ 比较数据表。

我已经尝试过:

var infoQuery =
                    (from db1 in GetTable1().AsEnumerable()
                     select db1).Except                            
                            (from db2 in GetTable2().AsEnumerable()
                             select db2);

也尝试过: (看起来应该和上面做的一样):

var results = GetTable1().AsEnumerable().Except(GetTable2().AsEnumerable());

我得到的结果是一张表中的所有记录。我正在寻找 1 的返回值,因为两个数据库之间有 1 行不同。

我正在使用 Object 中的默认 Equals 方法,我是否需要重写该实现才能使其正常工作?

I have two Databases. They are both filled into DataTables using the GetTable1 \ GetTable2 functions.

What I am looking to do is to basically compare the datatables using LINQ.

I Have tried :

var infoQuery =
                    (from db1 in GetTable1().AsEnumerable()
                     select db1).Except                            
                            (from db2 in GetTable2().AsEnumerable()
                             select db2);

Also tried :
(Looks like it should be doing same as above ) :

var results = GetTable1().AsEnumerable().Except(GetTable2().AsEnumerable());

The results I get back are all the records in one table. I am looking for a return of 1 since 1 row is different between the two databases.

I am using default Equals method from Object , do I need to override that implementation to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2024-12-02 21:43:54

它与行的比较方式有关。它们的值不会与参考值进行比较。

http://msdn.microsoft.com/en-us/library/bb300779.aspx

要比较自定义数据类型,请实现 IEquatable 通用接口并为该类型提供您自己的 GetHashCode 和 Equals 方法。默认相等比较器 Default 用于比较实现 IEquatable 的类型的值。

来自 MSDN 的示例:

public class Product : IEquatable<Product>
{
    public string Name { get; set; }
    public int Code { get; set; }

    public bool Equals(Product other)
    {

        //Check whether the compared object is null.
        if (Object.ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data.
        if (Object.ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal.
        return Code.Equals(other.Code) && Name.Equals(other.Name);
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public override int GetHashCode()
    {

        //Get hash code for the Name field if it is not null.
        int hashProductName = Name == null ? 0 : Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }
}

...

    Product[] fruits1 = { new Product { Name = "apple", Code = 9 }, 
                           new Product { Name = "orange", Code = 4 },
                            new Product { Name = "lemon", Code = 12 } };

    Product[] fruits2 = { new Product { Name = "apple", Code = 9 } };

    //Get all the elements from the first array
    //except for the elements from the second array.

    IEnumerable<Product> except =
        fruits1.Except(fruits2);

    foreach (var product in except)
        Console.WriteLine(product.Name + " " + product.Code);

    /*
      This code produces the following output:

      orange 4
      lemon 12
    */

It has to do with the way the rows are compared. Their values are not compared their references are.

http://msdn.microsoft.com/en-us/library/bb300779.aspx

To compare custom data types, implement the IEquatable generic interface and provide your own GetHashCode and Equals methods for the type. The default equality comparer, Default, is used to compare values of types that implement IEquatable.

Example from MSDN:

public class Product : IEquatable<Product>
{
    public string Name { get; set; }
    public int Code { get; set; }

    public bool Equals(Product other)
    {

        //Check whether the compared object is null.
        if (Object.ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data.
        if (Object.ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal.
        return Code.Equals(other.Code) && Name.Equals(other.Name);
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public override int GetHashCode()
    {

        //Get hash code for the Name field if it is not null.
        int hashProductName = Name == null ? 0 : Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }
}

...

    Product[] fruits1 = { new Product { Name = "apple", Code = 9 }, 
                           new Product { Name = "orange", Code = 4 },
                            new Product { Name = "lemon", Code = 12 } };

    Product[] fruits2 = { new Product { Name = "apple", Code = 9 } };

    //Get all the elements from the first array
    //except for the elements from the second array.

    IEnumerable<Product> except =
        fruits1.Except(fruits2);

    foreach (var product in except)
        Console.WriteLine(product.Name + " " + product.Code);

    /*
      This code produces the following output:

      orange 4
      lemon 12
    */
旧话新听 2024-12-02 21:43:54

即使它们可能具有相同的列,但 table1 中的单个数据行与 table2 中的行不匹配。所以 row_of_table1 != row_of_table2 始终为 true。您必须提供附加信息来比较它们(可能使用两个表都唯一的标识符)。 答案在这里。

Nate Zaugg 已经发布了它们不匹配的原因

Even though they might have the same columns a single data row from table1 does not match a row from table2. So row_of_table1 != row_of_table2 is always true. You must provide additional information to compare them ( maybe with an identifier that is unique to both tables ). One approach you could use as a starting point is described in an answer here.

The reason why they do not match is already posted by Nate Zaugg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文