如何解决当一个还没有 Id 时,C# 中是否有一种方法可以使用 EXCEPT 来比较 2 个列表?
我从数据库中得到 listA。我从最终用户上传的文件中获取 listB,我将其转换为列表中的正确类。例如,我给你一个来自数据库和用户上传的列表。您可以在下面找到这些示例。
我需要检查 listB 是否在 listA 中,但是当我使用 Except
时,我得到了整个列表,因为 Id 不在 listB 中(数据库中的自动编号)。我现在有一个解决方案,但有没有更好的方法来做到这一点?
到目前为止我尝试过的:
List<CustomerGroup> listC = listA.Except(listB).ToList();
//Returns listA in listC because Id isn't the same
这就是我现在用的,但看起来很多余。
foreach (CustomerGroup itemtocheck in listB)
{
var foundItem = listA.Find(dbItem => dbItem.FirstName== itemtocheck.FirstName &&
dbItem.FamilyName== itemtocheck.FamilyName &&
dbItem.Quantity == itemtocheck.Quantity &&
dbItem.discount == itemtocheck.discount);
if(foundItem != null)
{
listA.Remove(foundItem);
listB.Remove(itemtocheck);
}
}
foreach (CustomerGroup itemtocheck in listB)
{
// other code to check here
}
List<CustomerGroup> listA = new List<CustomerGroup>(){
new CustomerGroup {Id = 1,FirstName = "Anna",FamilyName = "Shrek",Quantity = 5,discount = 10},new CustomerGroup {Id = 2,FirstName = "Elsa",FamilyName = "Fiona",new CustomerGroup {Id = 3,FirstName = "Olaf",FamilyName = "Donkey",new CustomerGroup {Id = 4,FirstName = "sven",FamilyName = "Dragon",discount = 5},new CustomerGroup {Id = 5,FirstName = "Kristoff",FamilyName = "Puss",new CustomerGroup {Id = 6,Quantity = 10,discount = 15},new CustomerGroup {Id = 7,discount = 30}
};
List<CustomerGroup> listB = new List<CustomerGroup>(){
new CustomerGroup { FirstName = "Anna",new CustomerGroup { FirstName = "Elsa",discount = 8},new CustomerGroup { FirstName = "sven",new CustomerGroup { FirstName = "Kristoff",discount = 30},new CustomerGroup { FirstName = "Hans",FamilyName = "Farquaad",Quantity = 20,discount = 40}
};
public class CustomerGroup{
public int Id {get; set;}
public string FirstName {get; set;}
public string FamilyName {get; set;}
public int Quantity{get; set;}
public int discount {get; set;}
}
解决方法
您可以使用自己的 IEqualityComparer
,忽略 ID
属性:
class MyEqualityComparer : IEqualityComparer<CustomerGroup>
{
public bool Equals(CustomerGroup x,CustomerGroup y)
{
if(x is null) return y is null;
if(y is null) return false;
return x.FirstName == y.FirstName && x.FamilyName == y.FamilyName && x.Quantity == y.Quantity && x.Discount == y.Discount;
}
public int GetHashCode(CustomerGroup codeh)
{
return 0;
}
}
将此传递给Except
:
List<CustomerGroup> listC = listA.Except(listB,new MyEqualityComparer()).ToList();
在线演示:https://dotnetfiddle.net/gogy9g
,为类 false
实现 IEqualityComparer<>
,然后使用 CustomerGroup
,
喜欢
Except()
现在试试public class CompareCustomerGroup : IEqualityComparer<CustomerGroup>
{
public bool Equals(CustomerGroup dbCustomer,CustomerGroup uploadedCustomer)
{
if(dbCustomer == null || uploadedCustomer == null)
return false;
else
return dbCustomer.FirstName == uploadCustomer.FirstName &&
dbCustomer.FamilyName == uploadCustomer.FamilyName &&
dbCustomer.Quantity == uploadCustomer.Quantity &&
dbCustomer.Discount == uploadCustomer.Discount;
}
public int GetHashCode(CustomerGroup customGroup)
{
return HashCode.Combine(customGroup.FirstName,customGroup.FamilyName,customGroup.Quantity,customGroup.Discount);
}
}
,
Except()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。