Results 1 to 3 of 3

Thread: [RESOLVED] LINQ problems

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Resolved [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
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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
    Last edited by kareninstructor; Nov 21st, 2011 at 10:16 PM.

  3. #3

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: LINQ problems

    Nice, thanks for the example!
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width