使用主键检查VB.NET字典中是否存在对象

时间:2015-02-19 16:17:00

标签: vb.net linq dictionary

我有一个这样的词典:

Dim customersDictionary As Dictionary(Of Customer, IList(Of String))

Customer对象是关键,每个客户对象都有String值列表。

如何返回bool检查字典中是否存在id为123的客户?

1 个答案:

答案 0 :(得分:2)

如果您只是想知道您的词典中是否存在具有指定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
相关问题