【问题标题】:Comparing two generic lists on a specific property比较特定属性的两个通用列表
【发布时间】:2010-07-09 20:36:01
【问题描述】:

我正在使用带有 .NET 2.0 的 VB.NET。

我有两个列表,我想比较对象中特定属性的列表,而不是整个对象,并创建一个新列表,其中包含一个列表中的对象,而不是另一个列表中的对象。

myList1.Add(New Customer(1,"John","Doe")
myList1.Add(New Customer(2,"Jane","Doe")

myList2.Add(New Customer(1,"","")

上述示例中的结果将包含一位客户 Jane Doe,因为标识符 2 不在第二个列表中。

如何比较这两个 List<T> 或 .NET 2.0 中的任何 IEnumerable<T>(缺少 LINQ)?

【问题讨论】:

    标签: .net vb.net generics .net-2.0 list


    【解决方案1】:

    这是 C# 版本,VB 即将推出...

    Dictionary<int, bool> idMap = new Dictionary<int, bool>();
    myList2.ForEach(delegate(Customer c) { idMap[c.Id] = true; });
    
    List<Customer> myList3 = myList1.FindAll(
        delegate(Customer c) { return !idMap.ContainsKey(c.Id); });
    

    ...这是 VB 翻译。请注意,VB 在 .NET2 中没有匿名函数,因此使用 ForEachFindAll 方法会比标准的 For Each 循环更笨拙:

    Dim c As Customer
    
    Dim idMap As New Dictionary(Of Integer, Boolean)
    For Each c In myList2
        idMap.Item(c.Id) = True
    Next
    
    Dim myList3 As New List(Of Customer)
    For Each c In myList1
        If Not idMap.ContainsKey(c.Id) Then
            myList3.Add(c)
        End If
    Next
    

    【讨论】:

      【解决方案2】:

      这在 c# 2.0 中是可能的。

      List<Customer> results = list1.FindAll(delegate(Customer customer)
                                {
                                    return list2.Exists(delegate(Customer customer2)
                                                     {
                                                         return customer.ID == customer2.ID;
                                                     });
                                });
      

      【讨论】:

      • VB.NET (.NET 2.0) 可以吗?
      【解决方案3】:

      我想分享我的函数来比较两个对象列表:

      代码:

      Public Function CompareTwoLists(ByVal NewList As List(Of Object), ByVal OldList As List(Of Object))
                  If NewList IsNot Nothing AndAlso OldList IsNot Nothing Then
                      If NewList.Count <> OldList.Count Then
                          Return False
                      End If
                      For i As Integer = 0 To NewList.Count - 1
                          If Comparer.Equals(NewList(i), OldList(i)) = False Then
                              Return False
                          End If
                      Next
                  End If
                  Return True
      End Function
      

      例子:

      Dim NewList as new list (of string) from{"0","1","2","3"} 
      Dim OldList as new list (of string) from{"0","1","2","3"} 
      messagebox.show(CompareTwoLists(NewList,OldList)) 'return true 
      
      Dim NewList as new list (of string) from{"0","1","2","4"} 
      Dim OldList as new list (of string) from{"0","1","2","3"} 
      messagebox.show(CompareTwoLists(NewList,OldList)) 'return false
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 2019-07-11
        • 1970-01-01
        • 1970-01-01
        • 2011-06-12
        相关资源
        最近更新 更多