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

时间:2015-02-28 13:49:21

标签: c# linq except iequalitycomparer

我正在尝试学习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匹配不在testListBt4ProductID中的testListB匹配,但不具有相同的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表达式得到结果。我想使用工具IEqualityComparer但我不知道如何为两个不同类型的类实现GetHashCode它。因此,要么将上述查询组合到单个查询中,要么将任何其他方式合并为两个不同类型的类的自定义Comparer。还是有其他简单的方法吗?

3 个答案:

答案 0 :(得分:9)

您已经说过只需要testListA中的这些对象:

  • ProductID
  • 中未匹配testListB
  • 现有mathing 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();

答案 1 :(得分:0)

这是实现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();
  }
}

答案 2 :(得分:0)

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