use*_*246 5 c# linq iequalitycomparer except
我试图学习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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我为他们创建了两个列表,
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);
Run Code Online (Sandbox Code Playgroud)
现在的结果,我想t2因为它ProductID匹配不在testListB
而且t4因为它匹配ProductID的testListB,但不具有相同的Category.
1)我需要List<A>每条记录:不再ProductID在testListB中保留
我能得到的,
testListA.Select(x => x.ProductID).Except(testListB.Select(x => x.ProductID ));
Run Code Online (Sandbox Code Playgroud)
2)不再有一个匹配ProductID和Category在testListB中的记录
我可以使用,
testListA.Where(a => testListB.Any(b => a.ProductID == b.ProductID && a.Category != b.Category));
Run Code Online (Sandbox Code Playgroud)
**我的问题**
是否有可能两个单个linq表达式得到结果.IEqualityComparer我想使用该工具但我不知道如何GetHashCode为两种不同类型的类实现它.因此要么将上述查询组合成单个查询,要么以任何其他方式Comparer为两个不同类型的类实现自定义.或者还有其他简单的方法吗?
您已经说过只需要以下对象testListA:
ProductID的testListBProductID,但有不同的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));
Run Code Online (Sandbox Code Playgroud)
第二种方法:
或者您可以List<TestA>从第二个列表创建一个新的:
var secondListA = testListB.Select(x=> new TestA(){Category=x.Category, ProductID=x.ProductID}).ToList();
Run Code Online (Sandbox Code Playgroud)
然后创建你的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();
}
}
Run Code Online (Sandbox Code Playgroud)
并使用Except()指定IEqualityComparer<T>的比较值来使用生成两个序列的集合差异的重载:
var result = testListA.Except(secondListA, new MyComparer ()).ToList();
Run Code Online (Sandbox Code Playgroud)