|
-
Aug 24th, 2011, 05:07 AM
#1
Thread Starter
Fanatic Member
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
-
Aug 24th, 2011, 07:34 AM
#2
Thread Starter
Fanatic Member
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
-
Aug 24th, 2011, 07:51 AM
#3
Re: Struggling with OrderBy Lambda expression
 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.
-
Aug 24th, 2011, 08:00 AM
#4
Thread Starter
Fanatic Member
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
-
Aug 24th, 2011, 09:14 AM
#5
Re: Struggling with OrderBy Lambda expression
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|