Results 1 to 5 of 5

Thread: Struggling with OrderBy Lambda expression

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Struggling with OrderBy Lambda expression

    I'm getting my feet wet with Lambda expressions and it's rather overwhelming.

    I have a UserPrincipal object called SelectedUser. I wish to get a sorted list of the user's groups

    This is what I have so far but when I include that code my application's form flashes a few times and closes! Also, what is the significance of the "r" in the "Return From r" statement?

    Code:
    Dim SortedGroups As PrincipalSearchResult(Of Principal) = GetSortedGroups(Me.SelectedUser.GetGroups())
    
        Private Function GetSortedGroups(ByVal psp As PrincipalSearchResult(Of Principal)) As PrincipalSearchResult(Of Principal)
            Return From r In psp.OrderBy(Function(t) t.Name)
        End Function
    ManagePC - the all-in-one PC management and inventory tool

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Struggling with OrderBy Lambda expression

    Right, after some trial and error it's working. I had to cast it to an array

    Code:
        Private Function GetSortedGroups(ByVal psp As PrincipalSearchResult(Of Principal)) As Principal()
            Return (From r In psp.OrderBy(Function(grp) grp.Name)).ToArray()
        End Function
    Would someone mind explaining why this is so?
    ManagePC - the all-in-one PC management and inventory tool

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

    Re: Struggling with OrderBy Lambda expression

    Quote Originally Posted by Ginolard View Post
    Right, after some trial and error it's working. I had to cast it to an array

    Code:
        Private Function GetSortedGroups(ByVal psp As PrincipalSearchResult(Of Principal)) As Principal()
            Return (From r In psp.OrderBy(Function(grp) grp.Name)).ToArray()
        End Function
    Would someone mind explaining why this is so?
    You need to cast it as an Array because your return type Principal() is an array.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Struggling with OrderBy Lambda expression

    No, I meant why does the function have to return an array? Originally I was trying to return a PrincipalSearchResult collection which, obviously, wasn't working.

    LinQ is confusing
    ManagePC - the all-in-one PC management and inventory tool

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

    Re: Struggling with OrderBy Lambda expression

    Quote Originally Posted by Ginolard View Post
    No, I meant why does the function have to return an array? Originally I was trying to return a PrincipalSearchResult collection which, obviously, wasn't working.

    LinQ is confusing
    Did you try .ToList
    In regards to confusing, only when you do not take time to learn it rather then simply using LINQ.

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