比较特定属性的两个通用列表

发布于 2024-09-08 17:17:17 字数 426 浏览 2 评论 0原文

我正在使用 VB.NET 和 .NET 2.0。

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

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

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

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

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

I'm using VB.NET with .NET 2.0.

I have two lists, and I want to compare the lists on a specific property in the object, not the object as a whole, and create a new list that contains objects that are in one list, but not the other.

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

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

Result in the above example would contain one customer, Jane Doe, because the identifier 2 wasn't in the second list.

How can you compare these two List<T> or any IEnumerable<T> in .NET 2.0 (lacking LINQ)?

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

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

发布评论

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

评论(3

小草泠泠 2024-09-15 17:17:17

这是 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

Here's the C# version, VB coming up shortly...

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); });

...and here's the VB translation. Note that VB doesn't have anonymous functions in .NET2, so using the ForEach and FindAll methods would be more clumsy than standard For Each loops:

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
孤蝉 2024-09-15 17:17:17

这在 c# 2.0 中是可能的。

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

This is possible in c# 2.0.

List<Customer> results = list1.FindAll(delegate(Customer customer)
                          {
                              return list2.Exists(delegate(Customer customer2)
                                               {
                                                   return customer.ID == customer2.ID;
                                               });
                          });
放肆 2024-09-15 17:17:17

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

代码:

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

I want to share my function to compare two list of objects:

Code:

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

Example:

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