开发者

How can I search a List(of T) where T is a custom data class?

开发者 https://www.devze.com 2023-04-13 08:44 出处:网络
I think my question is a little more complicated than I can fit in the title. Say I have a data class for contacts:

I think my question is a little more complicated than I can fit in the title. Say I have a data class for contacts:

Public Class Contact
    Public Property Name As String
    Public Property Phone As String
    Public Property Fax As String
    Public Property Email As String
End Class

I have a List(Of Contact) object with several different contacts. How would I search the List for the contents 开发者_StackOverflow社区of one of the properties and return the resulting Contact object?


Linq is probably the simplest way. The matches variable is another List(Of Contact)

    Dim list As List(Of Contact) = {New Contact With {.Name = "Andy", .Phone = "1234", .Fax = "", .Email = ""}}

    Dim matches = From c In list
                  Where c.Phone = "1234"
                  Select c

Here's a good cheat sheet that I often refer to when trying to remember the basic syntax of things like this.

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html#arrays

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号