-
[RESOLVED] LINQ problems
I haven't done much with linq, and I've hit a block. I'm trying to get a collection of UserInformation. Then I plan on binding the collection to a drop down list. I'm stuck trying to convert the anonymous type returned from the query to a collection of UserInformation. I thought it might be something like my below code, but that doesn't work. "Extension method 'Public Function AsEnumerable() As System.Collections.Generic.IEnumerable(Of TSource)' defined in 'System.Linq.Enumerable' is not generic (or has no free type parameters) and so cannot have type arguments."
Code:
Public Shared ReadOnly Property AllUsers() As Generic.IEnumerable(Of UserInformation)
Get
Dim linqQuery = From u In Membership.GetAllUsers Select UserId, User.Email
'UserInformation class has UserId and Email properties
Return linqQuery.AsEnumerable(Of UserInformation)()
End Get
End Property
-
Re: LINQ problems
The following is in VS2010 syntax, perhaps this might work for you.
Code:
Private Users As New List(Of UserInformation)
Public ReadOnly Property AllItems As List(Of UserInformation)
Get
Return (From U In Users
Select New UserInformation(U.UserId, U.Email)).ToList
End Get
End Property
Private Sub Form1_Load() Handles MyBase.Load
Users.Add(New UserInformation("FirstUser", "f.comcast.net"))
Users.Add(New UserInformation("SecondUser", "g.comcast.net"))
ComboBox1.DisplayMember = "UserId"
ComboBox1.ValueMember = "Email"
ComboBox1.DataSource = AllItems
End Sub
Private Sub ComboBox1_SelectedIndexChanged(
ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Console.WriteLine(DirectCast(ComboBox1.SelectedItem,
UserInformation).Email)
End Sub
Code:
Class UserInformation
Public Property UserId As String
Public Property Email As String
Public Sub New(ByVal UserId As String, ByVal Email As String)
Me.UserId = UserId
Me.Email = Email
End Sub
End Class
-
Re: LINQ problems
Nice, thanks for the example!