比较列表并返回不存在的对象

发布于 2024-11-03 00:50:14 字数 1931 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半山落雨半山空 2024-11-10 00:50:14
 var zipCodes = subList.Select( c=> c.ZipCode).Distinct().ToList();
 var v = allCustomers.Where( c=> zipCodes.Contains(c.ZipCode) )
                     .Except(subList)
                     .ToList();
 var zipCodes = subList.Select( c=> c.ZipCode).Distinct().ToList();
 var v = allCustomers.Where( c=> zipCodes.Contains(c.ZipCode) )
                     .Except(subList)
                     .ToList();
執念 2024-11-10 00:50:14

为什么你说 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文