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
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?
Re: Struggling with OrderBy Lambda expression
Quote:
Originally Posted by
Ginolard
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.
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 :(
Re: Struggling with OrderBy Lambda expression
Quote:
Originally Posted by
Ginolard
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.