Hi Ron,
You can use projection to select only the "column" or property of an object. You can then use FirstOrDefault (or First) to only get a single value. The following code would store Ron in the singleFirstName.FirstName variable:
Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim list As New List(Of Person) From {New Person() With {.FirstName = "Imar", .LastName = "Spaanjaars"}, New Person() With {.FirstName = "Ron", .LastName = "Howerton"}}
Dim singleFirstName = (From p In list
Where p.LastName = "Howerton"
Select New With {.FirstName = p.FirstName}).FirstOrDefault()
' singleFirstName.FirstName = Ron
End Sub
End Class
Public Class Person
Public Property FirstName As String
Public Property LastName As String
End Class
This code "projects" the first name property into a new, anonymous object with a single property called FirstName.
You can also assign it to a string variable directly by just selecting the FirstName field, like this:
Code:
Dim singleFirstName = (From p In list
Where p.LastName = "Howerton"
Select p.FirstName).FirstOrDefault()
If you anticipate multiple results, drop the call to FirstOrDefault and loop over the values in singleFirstName (which then of course has an odd name ;-) ).
Hope this helps,
Imar