LINQ: How To Make Order By Conditional?
-
I'm trying to figure out how to make the Ascending / Descending extension in an Order By statement in a Linq query conditional based upon a menu selection. Here's the query:
Dim fileItem = From files In itemList
Where files.Tag(0) = "file"
Order By files.SubItems.Item(indx).Text 'Ascending or Descending dependent upon menu selection
Select files(itemList is a List of listviewitems) Any advice is appreciated.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
I'm trying to figure out how to make the Ascending / Descending extension in an Order By statement in a Linq query conditional based upon a menu selection. Here's the query:
Dim fileItem = From files In itemList
Where files.Tag(0) = "file"
Order By files.SubItems.Item(indx).Text 'Ascending or Descending dependent upon menu selection
Select files(itemList is a List of listviewitems) Any advice is appreciated.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
Not exactly what I was hunting since the menu item isn't a part of the collection. But no big thing. I finally just added code to reverse the collection if I needed it sorted in descending order.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
I'm trying to figure out how to make the Ascending / Descending extension in an Order By statement in a Linq query conditional based upon a menu selection. Here's the query:
Dim fileItem = From files In itemList
Where files.Tag(0) = "file"
Order By files.SubItems.Item(indx).Text 'Ascending or Descending dependent upon menu selection
Select files(itemList is a List of listviewitems) Any advice is appreciated.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
Simple:
Dim fileItem = From files In itemList
Where files.Tag(0) = "file"
Select filesIf shouldSortDescending Then
fileItem = From files In fileItem
Order By files.SubItems.Item(indx).Text Descending
Select files
Else
fileItem = From files In fileItem
Order By files.SubItems.Item(indx).Text
Select files
End IfOr, using "method syntax":
Dim fileItem = itemList.Where(Function (files) files.Tag(0) = "file")
If shouldSortDescending Then
fileItem = fileItem.OrderByDescending(Function (files) files.SubItems.Item(indx).Text)
Else
fileItem = fileItem.OrderBy(Function (files) files.SubItems.Item(indx).Text)
End If
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Simple:
Dim fileItem = From files In itemList
Where files.Tag(0) = "file"
Select filesIf shouldSortDescending Then
fileItem = From files In fileItem
Order By files.SubItems.Item(indx).Text Descending
Select files
Else
fileItem = From files In fileItem
Order By files.SubItems.Item(indx).Text
Select files
End IfOr, using "method syntax":
Dim fileItem = itemList.Where(Function (files) files.Tag(0) = "file")
If shouldSortDescending Then
fileItem = fileItem.OrderByDescending(Function (files) files.SubItems.Item(indx).Text)
Else
fileItem = fileItem.OrderBy(Function (files) files.SubItems.Item(indx).Text)
End If
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Here Alan was looking for some Linq magic (so was I) and you give him a simple if statement. :laugh: :laugh: :laugh: KISS is alive and well in CP!
Never underestimate the power of human stupidity RAH
-
Here Alan was looking for some Linq magic (so was I) and you give him a simple if statement. :laugh: :laugh: :laugh: KISS is alive and well in CP!
Never underestimate the power of human stupidity RAH
You could always create your own extension method. :)
Module SortingExtensions
<Extension()>
Public Function OrderBy(Of TElement, TKey)(
ByVal source As IEnumerable(Of TElement),
ByVal selector As Func(Of TElement, TKey),
ByVal sortDescending As Boolean) _
As IOrderedEnumerable(Of TElement)Return If(sortDescending, source.OrderByDescending(selector), source.OrderBy(selector)) End Function <Extension()> Public Function ThenBy(Of TElement, TKey)( ByVal source As IOrderedEnumerable(Of TElement), ByVal selector As Func(Of TElement, TKey), ByVal sortDescending As Boolean) \_ As IOrderedEnumerable(Of TElement) Return If(sortDescending, source.ThenByDescending(selector), source.ThenBy(selector)) End Function
End Module
...
Dim fileItem = itemList.Where(Function (files) files.Tag(0) = "file").OrderBy(Function (files) files.SubItems.Item(indx).Text, shouldSortDescending)
There's no option to extend the query syntax though.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Here Alan was looking for some Linq magic (so was I) and you give him a simple if statement. :laugh: :laugh: :laugh: KISS is alive and well in CP!
Never underestimate the power of human stupidity RAH
:thumbsup::thumbsup::thumbsup:
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
Simple:
Dim fileItem = From files In itemList
Where files.Tag(0) = "file"
Select filesIf shouldSortDescending Then
fileItem = From files In fileItem
Order By files.SubItems.Item(indx).Text Descending
Select files
Else
fileItem = From files In fileItem
Order By files.SubItems.Item(indx).Text
Select files
End IfOr, using "method syntax":
Dim fileItem = itemList.Where(Function (files) files.Tag(0) = "file")
If shouldSortDescending Then
fileItem = fileItem.OrderByDescending(Function (files) files.SubItems.Item(indx).Text)
Else
fileItem = fileItem.OrderBy(Function (files) files.SubItems.Item(indx).Text)
End If
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks Richard. I ended up writing something similar to the "Simple" version. I've been shamelessly flip-flopping around on this thing for a month - good thing it's just for me and not a customer :-D.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.