我已经按照这篇文章的建议尝试让 Distinct() 在我的代码中工作,但我仍然遇到问题。这是我正在使用的两个对象:
public class InvoiceItem : IEqualityComparer<InvoiceItem>
{
public InvoiceItem(string userName, string invoiceNumber, string invoiceAmount)
{
this.UserName = userName;
this.InvoiceNumber= invoiceNumber;
this.InvoiceAmount= invoiceAmount;
}
public string UserName { get; set; }
public string InvoiceNumber { get; set; }
public double InvoiceAmount { get; set; }
public bool Equals(InvoiceItem left, InvoiceItem right)
{
if ((object)left.InvoiceNumber == null && (object)right.InvoiceNumber == null) { return true; }
if ((object)left.InvoiceNumber == null || (object)right.InvoiceNumber == null) { return false; }
return left.InvoiceNumber == right.InvoiceNumber;
}
public int GetHashCode(InvoiceItem item)
{
return item.InvoiceNumber == null ? 0 : item.InvoiceNumber.GetHashCode();
}
}
public class InvoiceItems : List<InvoiceItem>{ }
我的目标是用几千个对象填充一个InvoiceItems
对象(我们称之为) ,然后执行:aBunchOfInvoiceItems
InvoiceItem
InvoiceItems distinctItems = aBunchOfInvoiceItems.Distinct();
当我设置并运行此代码时,我收到一条错误消息
无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“InvoiceReader.Form1.InvoiceItems”。存在显式转换(您是否缺少演员表?)
我不明白如何解决这个问题。我应该采取不同的方法吗?非常感谢任何建议。