C# 复杂对象比较

发布于 2024-09-15 00:08:29 字数 781 浏览 12 评论 0原文

public class SecurityMaster : EntityObject
     {
        public string BondIdentifier { get; set; }
        public string Description { get; set; }
         public EntityCollection<SecurityMasterSchedules> SecurityMasterSchedules { get; set}
      }
     public class SecurityMasterSchedules :EntityObject
     {
           public string BondIdentifier { get; set; }
           public decimal rate { get; set; }
           public datetime startdate { get; set; }
           public datetime endate { get; set; }
     }

如何比较对象列表list1和列表list2? IEnumerable except 方法不适用于复杂对象。

   List<SecurityMaster> list1 = new List<SecurityMaster>();
   List<SecurityMaster> list2 = new List<SecurityMaster>();
public class SecurityMaster : EntityObject
     {
        public string BondIdentifier { get; set; }
        public string Description { get; set; }
         public EntityCollection<SecurityMasterSchedules> SecurityMasterSchedules { get; set}
      }
     public class SecurityMasterSchedules :EntityObject
     {
           public string BondIdentifier { get; set; }
           public decimal rate { get; set; }
           public datetime startdate { get; set; }
           public datetime endate { get; set; }
     }

How do I compare the objects List list1 and List list2? IEnumerable except method doesn't work with complex objects.

   List<SecurityMaster> list1 = new List<SecurityMaster>();
   List<SecurityMaster> list2 = new List<SecurityMaster>();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

予囚 2024-09-22 00:08:29

我猜您有两个 IEnumerable 序列,并且您想知道如何使用 Except 将两个 EntityObjects 视为在特定标准下相同。我将进一步猜测这些标准包含一个简单的规则(只是为了让我自己在提供这个答案时更轻松):具有相同 BondIdentifier 属性的两个项目将被视为相同。

好吧,那么,使用接受 IEqualityComparerExcept 重载:

class EntityObjectComparer : IEqualityComparer<EntityObject>
{
    public bool Equals(EntityObject x, EntityObject y)
    {
        string xId = GetBondIdentifier(x);
        string yId = GetBondIdentifier(y);

        return x.Equals(y);
    }

    private string GetBondIdentifier(EntityObject obj)
    {
        var sm = obj as SecurityMaster;
        if (sm != null)
        {
            return sm.BondIdentifier;
        }

        var sms = obj as SecurityMasterSchedules;
        if (sms != null)
        {
            return sms.BondIdentifier;
        }

        return string.Empty;
    }
}

List<EntityObject> list1 = GetList1();
List<EntityObject> list2 = GetList2();

var itemsInList1NotInList2 = list1.Except(list2, new EntityObjectComparer());

即使我对标准的猜测是错误的,您仍然可以使用这个答案作为以此为基础来制定自己的。

如果我最初的猜测也是错误的,那么显然这个答案对你来说毫无用处。

I'm guessing you have two IEnumerable<EntityObject> sequences and you want to know how to use Except in a way that treats two EntityObjects as identical under specific criteria. I'm going to further guess that these criteria comprise one simple rule (just to make life easier for myself in providing this answer): two items with the same BondIdentifier property will be considered identical.

Well, then, use the overload for Except that accepts an IEqualityComparer<T>:

class EntityObjectComparer : IEqualityComparer<EntityObject>
{
    public bool Equals(EntityObject x, EntityObject y)
    {
        string xId = GetBondIdentifier(x);
        string yId = GetBondIdentifier(y);

        return x.Equals(y);
    }

    private string GetBondIdentifier(EntityObject obj)
    {
        var sm = obj as SecurityMaster;
        if (sm != null)
        {
            return sm.BondIdentifier;
        }

        var sms = obj as SecurityMasterSchedules;
        if (sms != null)
        {
            return sms.BondIdentifier;
        }

        return string.Empty;
    }
}

List<EntityObject> list1 = GetList1();
List<EntityObject> list2 = GetList2();

var itemsInList1NotInList2 = list1.Except(list2, new EntityObjectComparer());

Even if my guess as to the criteria was wrong, you can still use this answer as a basis from which to formulate your own.

If my initial guess was also wrong, then clearly this answer is useless to you.

浸婚纱 2024-09-22 00:08:29

我不确定您的代码与什么有关,但这是您使用 例外

var list1 = new int[] { 1, 2, 3 };
var list2 = new int[] { 1 };

var output = list1.Except(list2).First();

Console.WriteLine(output);  // prints "2"

I'm not sure what your code has to do with anything, but here's how you use Except:

var list1 = new int[] { 1, 2, 3 };
var list2 = new int[] { 1 };

var output = list1.Except(list2).First();

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