使用主键检查对象是否存在于 VB.NET 字典中
Check if object exists in a VB.NET dictionary using Primary Key
我有一个这样声明的字典:
Dim customersDictionary As Dictionary(Of Customer, IList(Of String))
客户对象是键,每个客户对象都有一个字符串值列表。
我如何 return bool 检查字典中是否存在 ID 为 123 的客户?
如果您只想知道您的字典中是否存在具有指定 ID 的客户,那么您可以写
dim result = custList.Any(Function(x) x.Key.ID = 123)
if result then
Console.WriteLine("Customer with ID=123 exists")
End if
相反,如果您想检索给定 ID 的客户,则
dim result = custList.Where(Function(x) x.Key.ID = 123).FirstOrDefault()
if result.Key IsNot Nothing Then
Dim cust = result.Key
Console.WriteLine("Customer Name is = " & cust.Name) ' or whatever property of your customer
End If
我有一个这样声明的字典:
Dim customersDictionary As Dictionary(Of Customer, IList(Of String))
客户对象是键,每个客户对象都有一个字符串值列表。
我如何 return bool 检查字典中是否存在 ID 为 123 的客户?
如果您只想知道您的字典中是否存在具有指定 ID 的客户,那么您可以写
dim result = custList.Any(Function(x) x.Key.ID = 123)
if result then
Console.WriteLine("Customer with ID=123 exists")
End if
相反,如果您想检索给定 ID 的客户,则
dim result = custList.Where(Function(x) x.Key.ID = 123).FirstOrDefault()
if result.Key IsNot Nothing Then
Dim cust = result.Key
Console.WriteLine("Customer Name is = " & cust.Name) ' or whatever property of your customer
End If