[英]C# Compare two list of same object type
I am trying to compare two list of same type using multiple properties of that type. 我正在尝试使用该类型的多个属性比较两个相同类型的列表。
For example, 例如,
I have a class named Details 我有一个名为Details的类
public class Details
{
public int id;
public string symbol;
public string code;
}
I have below two lists: 我有以下两个列表:
List<Details> list1 = new List<Details>();
List<Details> list2 = new List<Details>();
list1.Add(new Details() { id=1,symbol="ANSI",code="NITE"});
list1.Add(new Details() { id = 1, symbol = "ANSI", code = "CALGO" });
list1.Add(new Details() { id = 1, symbol = "ANSI", code = "CANT" });
list1.Add(new Details() { id=2,symbol="ANSI",code="NITE"});
list1.Add(new Details() { id = 2, symbol = "ANSI", code = "CALGO" });
list1.Add(new Details() { id = 2, symbol = "ANSI", code = "CANT" });
list2.Add(new Details() { id = 1, symbol = "ANSI", code = "NITE" });
list2.Add(new Details() { id = 1, symbol = "ANSI", code = "CALGO" });
list2.Add(new Details() { id = 2, symbol = "ANSI", code = "NITE" });
I want only that data from List1 which has same id, symbol but different code. 我只想要来自List1的具有相同ID,符号但不同代码的数据。
So, in above scenario result will be as below. 因此,在上述情况下结果将如下所示。
list1.Add(new Details() { id = 1, symbol = "ANSI", code = "CANT" });
list1.Add(new Details() { id = 2, symbol = "ANSI", code = "CALGO" });
list1.Add(new Details() { id = 2, symbol = "ANSI", code = "CANT" });
It would be great if this can be achieved through Linq instead of using foreach. 如果可以通过Linq而不是使用foreach来实现,那就太好了。
I tried below but that's not correct. 我在下面尝试过,但这是不正确的。
var temp =list1.Where(x=>list2.Any(z=>x.id==z.id && string.Equals(x.symbol,z.symbol) && !string.Equals(x.code,z.code)));
It looks like you need rows to satisfy two conditions, not one, in order to make the output: 看起来您需要满足两个条件而不是一个条件的行才能生成输出:
id
and symbol
, and id
和symbol
必须匹配,并且 id
, symbol
, and code
. id
, symbol
和code
不得匹配。 Here is how to do that with LINQ directly: 这是直接使用LINQ的方法:
var tmp = list1.Where(x=>
list2.Any(z=>x.id==z.id && x.symbol==z.symbol)
&& !list2.Any(z => x.id==z.id && x.symbol==z.symbol && x.code==z.code));
An alternative based on applying De Morgan's laws : 基于应用戴摩根定律的另一种选择:
var tmp = list1.Where(x=>
list2.Any(z=>x.id==z.id && x.symbol==z.symbol)
&& list2.All(z => x.id!=z.id || x.symbol!=z.symbol || x.code!=z.code));
1) I would first override Equals
(and also GetHashCode
) 1)我将首先覆盖Equals
(以及GetHashCode
)
public class Details
{
public int id;
public string symbol;
public string code;
public override int GetHashCode()
{
return (id + symbol + code).GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as Details;
if (other == null) return false;
return id == other.id && symbol == other.symbol && code == other.code;
}
}
Then you can use Linq as 然后您可以使用Linq作为
var result = list1.Except(list2).ToList();
It returns the result you expect... 它返回您期望的结果...
2) Same result can also be obtained without changing the Details
object and by implementing a custom IEqualityComparer
2)在不更改Details
对象和实现自定义IEqualityComparer
情况下,也可以获得相同的结果
public class DetailsComparer : IEqualityComparer<Details>
{
public bool Equals(Details x, Details y)
{
return x.id == y.id && x.symbol == y.symbol && x.code == y.code;
}
public int GetHashCode(Details obj)
{
return (obj.id + obj.symbol + obj.code).GetHashCode();
}
}
Then your linq would be 然后你的linq将是
var result = list1.Except(list2, new DetailsComparer()).ToList();
Those ways are better than O(n*n) algorithms utilizing of Any
and All
这些方法比利用Any
和All
O(n * n)算法要好
of course, you can do compare like your code, but if you want your code are more structure you can override method Equals()
and Operator ==
: 当然,您可以像代码一样进行比较,但是如果您希望代码更具结构性,则可以覆盖方法Equals()
和Operator ==
:
public class Details
{
public int id;
public string symbol;
public string code;
public override bool Equals(System.Object obj)
{
if (obj == null) {
return false;
}
Details detail = obj as Details;
if ((System.Object) detail == null) {
return false;
}
return (id == detail.id) && (symbol == detail.symbol);
}
public bool Equals(other) {
return this.id == other.id && this.symbole == other.symbol;
}
public override int GetHashCode() {
return id ^ symbol.GetHashCode();
}
}
then you can compare two Detail object directly. 那么您可以直接比较两个Detail对象。
Do this: 做这个:
public class DetailsComparer : IEqualityComparer<Details>
{
public bool Equals(Details x, Details y)
=> x.id == y.id && x.symbol == y.symbol && x.code == y.code;
public int GetHashCode(Details obj)
=> obj.code.GetHashCode();
}
And use wery simple this 并使用简单
var x = list1.Except(list2, new DetailsComparer());
result x: 结果x:
1, ANSI, CANT 1,ANSI,CANT
2, ANSI, CALGO 2,ANSI,CALGO
2, ANSI, CANT 2,ANSI,CANT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.