Dim receipts As IQueryable(Of ReceiptEntity) = db.Receipts
'code to filter removed for brevity
Dim sorts() As String = SortExpression.Split(";")
For Each sort As String In sorts
Dim sortParts() As String = sort.Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.OrderBy(Of ReceiptEntity)(sortParts(0).ToString(), SortDirection.Ascending)
Else
receipts = receipts.OrderBy(Of ReceiptEntity)(sortParts(0).ToString(), SortDirection.Descending)
End If
Next
SortExpression 像“field1 true;field2 false;field3 true”一样出现
我想要发生的是查询具有多个 order by 字段,正在发生的事情是只应用最后一个 order by。我在这里做错了什么?
这是工作结果的样子:
Dim receipts As IOrderedQueryable(Of ReceiptEntity) = db.Receipts.Include(Function(r) r.LineItems).Include(Function(r) r.Payments)
Dim sorts() As String = SortExpression.Split(";")
Dim sortParts() As String
sortParts = sorts(0).Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.OrderBy(sortParts(0).ToString())
Else
receipts = receipts.OrderByDescending(sortParts(0).ToString())
End If
For Each sort As String In sorts.Skip(1)
sortParts = sort.Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.ThenBy(sortParts(0).ToString())
Else
receipts = receipts.ThenByDescending(sortParts(0).ToString())
End If
Next