比较列表并返回不存在的对象
class Customer : IEquatable<Customer>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string ZipCode { get; set; }
public bool Equals(Customer other)
{
if (ReferenceEquals(null, other)) return false;
return this.FirstName.Equals(other.FirstName) && this.LastName.Equals(other.LastName)
&& this.ZipCode.Equals(other.ZipCode);
}
public override bool Equals(object obj)
{
return this.Equals(obj as Customer);
}
public override int GetHashCode()
{
return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode();
}
}
static void Main(string[] args)
{
List<Customer> allCustomers = new List<Customer>();
allCustomers.Add(new Customer { FirstName = "A", LastName = "V", ZipCode = "11111" });
allCustomers.Add(new Customer { FirstName = "B", LastName = "W", ZipCode = "11111" });
allCustomers.Add(new Customer { FirstName = "C", LastName = "X", ZipCode = "22222" });
allCustomers.Add(new Customer { FirstName = "D", LastName = "Y", ZipCode = "33333" });
allCustomers.Add(new Customer { FirstName = "E", LastName = "Z", ZipCode = "33333" });
List<Customer> subList = new List<Customer>();
subList.Add(new Customer { FirstName = "A", LastName = "V", ZipCode = "11111" });
subList.Add(new Customer { FirstName = "B", LastName = "W", ZipCode = "11111" });
subList.Add(new Customer { FirstName = "C", LastName = "X", ZipCode = "22222" });
//This gives expected answer
var n = subList.Except(allCustomers).ToList();
//This should compare only for those customers who zip matches with Sublist's zip
//This returns customers with zip code "33333" while it should not
var v = allCustomers.Except(subList).ToList();
}
var V 不应返回邮政编码为“33333”的客户。我如何比较才能忽略那些 Zip 不在子列表中的客户?
class Customer : IEquatable<Customer>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string ZipCode { get; set; }
public bool Equals(Customer other)
{
if (ReferenceEquals(null, other)) return false;
return this.FirstName.Equals(other.FirstName) && this.LastName.Equals(other.LastName)
&& this.ZipCode.Equals(other.ZipCode);
}
public override bool Equals(object obj)
{
return this.Equals(obj as Customer);
}
public override int GetHashCode()
{
return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode();
}
}
static void Main(string[] args)
{
List<Customer> allCustomers = new List<Customer>();
allCustomers.Add(new Customer { FirstName = "A", LastName = "V", ZipCode = "11111" });
allCustomers.Add(new Customer { FirstName = "B", LastName = "W", ZipCode = "11111" });
allCustomers.Add(new Customer { FirstName = "C", LastName = "X", ZipCode = "22222" });
allCustomers.Add(new Customer { FirstName = "D", LastName = "Y", ZipCode = "33333" });
allCustomers.Add(new Customer { FirstName = "E", LastName = "Z", ZipCode = "33333" });
List<Customer> subList = new List<Customer>();
subList.Add(new Customer { FirstName = "A", LastName = "V", ZipCode = "11111" });
subList.Add(new Customer { FirstName = "B", LastName = "W", ZipCode = "11111" });
subList.Add(new Customer { FirstName = "C", LastName = "X", ZipCode = "22222" });
//This gives expected answer
var n = subList.Except(allCustomers).ToList();
//This should compare only for those customers who zip matches with Sublist's zip
//This returns customers with zip code "33333" while it should not
var v = allCustomers.Except(subList).ToList();
}
var V is not supposed to return customer with zipcode "33333". How do i compare so that it ignores those Customers whose Zip is not present in the Sublist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么你说 V 不应该包含邮政编码 33333? allCustomers - subList 将删除公共元素,但保留 allCustomers 的独特元素。
这是一个集合差异,因此 {A, B, C, D, E} - {A, B, C} = {D, E} 因此 33333 应该显示...
看起来与 MSDN 中的这个示例类似: http://msdn.microsoft.com/en-us/library/bb300779.aspx
Why are you saying V should not include zipcode 33333? allCustomers - subList would remove the common elements but keep the unique elements of allCustomers.
It's a set difference, so {A, B, C, D, E} - {A, B, C} = {D, E} thus the 33333 should show...
Seems similar to this example from MSDN: http://msdn.microsoft.com/en-us/library/bb300779.aspx