开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜