如何使用具有不同 class 的多个属性的 linq `Except`?

How to use linq `Except` with multiple properties with different class?

我正在尝试学习 Linq/Lambda 表达式,但卡在了某个地方。

我在做什么

我创建了两个 类,其中的属性具有一些共同的属性。 类 就像(这是测试代码)。

class TestA
    {
        public int Id { get; set; }
        public int ProductID { get; set; }
        public string Category { get; set; }

        public TestA(int id, int procid, string category)
        {
            this.Id = id;
            this.ProductID = procid;
            this.Category = category;
        }
    }

    class TestB
    {
        public int ProductID { get; set; }
        public string Category { get; set; }

        public TestB(int procid, string category)
        {
            this.ProductID = procid;
            this.Category = category;
        }
    }

然后我为他们创建了两个列表,

        List<TestA> testListA = new List<TestA>();
        List<TestB> testListB = new List<TestB>();
        TestA t1 = new TestA(1, 254, "ProductA");
        TestA t2 = new TestA(1, 236, "ProductA");
        TestA t3 = new TestA(1, 215, "ProductB");
        TestA t4 = new TestA(1, 175, "ProductB");
        TestA t5 = new TestA(1, 175, "ProductC");
        testListA.Add(t1);
        testListA.Add(t2);
        testListA.Add(t3);
        testListA.Add(t4);
        testListA.Add(t5);

        TestB tt1 = new TestB(254, "ProdcutA");
        TestB tt2 = new TestB(215, "ProductB");
        TestB tt3 = new TestB(175, "ProductC");
        testListB.Add(tt3);
        testListB.Add(tt2);
        testListB.Add(tt1);

现在我想要 t2 的结果,因为 ProductID 匹配项不在 testListB 中 和 t4 因为它在 testListB 中有匹配的 ProductID 但没有相同的 Category.
1) 我需要每条记录的 List<A>,其中: testListB

中不再有 ProductID

我可以得到,

  testListA.Select(x => x.ProductID).Except(testListB.Select(x => x.ProductID ));

2)在 testListB

中不再有匹配 ProductIDCategory 的记录

我可以使用,

   testListA.Where(a => testListB.Any(b => a.ProductID == b.ProductID && a.Category != b.Category));

**我的问题**
是否有可能两个使单个 linq 表达式得到 result.I 考虑使用工具 IEqualityComparer 但我不确定如何为两种不同类型的 类 实现 GetHashCode .因此,要么将上述查询组合成单个查询,要么以任何其他方式为两种不同类型的 类 实现自定义 Comparer。或者有什么其他简单的方法吗?

您说过您只需要 testListA:

中的这些对象
  • testListB
  • 中没有匹配的ProductID
  • 有现有的数学计算 ProductID,但有不同的 Category

因此,您的过滤器必须是:

!testListB.Any(b => a.ProductID == b.ProductID && a.Category == b.Category)

因此,将您的代码更改为:

testListA.Where(a => !testListB.Any(b => a.ProductID == b.ProductID && a.Category == b.Category));

第二种方法:

或者您可以从第二个列表创建一个新的 List<TestA>

 var secondListA = testListB.Select(x=> new TestA(){Category=x.Category, ProductID=x.ProductID}).ToList();

然后创建你的 Comparer:

sealed class MyComparer : IEqualityComparer<TestA>
{
    public bool Equals(TestA x, TestA y)
    {
        if (x == null)
            return y == null;
        else if (y == null)
            return false;
        else
            return x.ProductID == y.ProductID && x.Category == y.Category;
    }

    public int GetHashCode(TestA obj)
    {
        return obj.ProductID.GetHashCode();
    }
}

并使用Except()重载,通过使用指定的IEqualityComparer<T>比较值来产生两个序列的集合差异。:

var result = testListA.Except(secondListA, new MyComparer ()).ToList();

这是实施 IEqualityComparer 的示例。它应该作为您的目的的概念:

private class PKExclusiveComparer<T> : IEqualityComparer<T> where T : DataRow
{
  public bool Equals(T x, T y)
  {
    bool result = true;
    foreach (DataColumn col in (x as DataRow).Table.Columns)
    {
      if (!(x as DataRow).Table.PrimaryKey.Contains(col))
      {
        result &= x[col].Equals(y[col]);
      }
    }
    return result;
  }

  public int GetHashCode(T x)
  {
    string code = string.Empty;
    foreach (DataColumn col in (x as DataRow).Table.Columns)
    {
      if (!(x as DataRow).Table.PrimaryKey.Contains(col))
      {
        code += x[col].ToString();
      }
    }
    return code.GetHashCode();
  }
}
Public Class dosyalar
    Public adi As String
    Public tarih As DateTime
End Class


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim dosyalarList1 As New List(Of dosyalar)
    dosyalarList1.Add(New dosyalar With {.adi = "dosya1", .tarih = New DateTime(2020, 3, 30, 10, 10, 10)})
    dosyalarList1.Add(New dosyalar With {.adi = "dosya2", .tarih = New DateTime(2020, 3, 30, 10, 10, 20)})
    dosyalarList1.Add(New dosyalar With {.adi = "dosya3", .tarih = New DateTime(2020, 3, 30, 10, 10, 30)})
    dosyalarList1.Add(New dosyalar With {.adi = "dosya4", .tarih = New DateTime(2020, 3, 30, 10, 10, 40)})
    dosyalarList1.Add(New dosyalar With {.adi = "dosya5", .tarih = New DateTime(2020, 3, 30, 10, 10, 50)})

    Dim dosyalarList2 As New List(Of dosyalar)
    dosyalarList2.Add(New dosyalar With {.adi = "dosya1", .tarih = New DateTime(2020, 3, 30, 10, 10, 11)})
    dosyalarList2.Add(New dosyalar With {.adi = "dosya2", .tarih = New DateTime(2020, 3, 30, 10, 10, 21)})
    dosyalarList2.Add(New dosyalar With {.adi = "dosya3", .tarih = New DateTime(2020, 3, 30, 10, 10, 30)})
    dosyalarList2.Add(New dosyalar With {.adi = "dosya4", .tarih = New DateTime(2020, 3, 30, 10, 10, 40)})
    dosyalarList2.Add(New dosyalar With {.adi = "dosya5", .tarih = New DateTime(2020, 3, 30, 10, 10, 50)})


    Dim result As List(Of dosyalar) = dosyalarList1.Where(Function(a) Not dosyalarList2.Any(Function(b) a.adi = b.adi AndAlso a.tarih = b.tarih)).ToList

End Sub