รูปแบบการเรียกใช้งานทำได้ดังนี้
list.OrderBy("SomeProperty");
list.OrderBy("SomeProperty DESC");
list.OrderBy("SomeProperty DESC, SomeOtherProperty");
list.OrderBy("SomeSubObject.SomeProperty ASC, SomeOtherProperty DESC");
ที่มา
Dynamic SQL-like Linq OrderBy Extension
http://aonnull.blogspot.com/2010/08/dynamic-sql-like-linq-orderby-extension.html
C#.net Convert To VB.net By :http://converter.telerik.com/
View Code
-------------------------------------------------
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Linq.Expressions
Imports System.Reflection
Module OrderByExtensions
'' Mutiple Sort in Linq Order by String example :"sAccount,CardID desc"
'' objdata.OrderBy("sAccount asc") ,objdata.OrderBy("sAccount asc,CardID desc")
<System.Runtime.CompilerServices.Extension()> _
Public Function OrderBy(Of T)(enumerable As IEnumerable(Of T), strorderBy As String) As IEnumerable(Of T)
Return enumerable.AsQueryable().OrderBy(strorderBy).AsEnumerable()
End Function
<System.Runtime.CompilerServices.Extension()> _
Public Function OrderBy(Of T)(collection As IQueryable(Of T), strorderBy As String) As IQueryable(Of T)
For Each orderByInfo As OrderByInfo In ParseOrderBy(strorderBy)
collection = ApplyOrderBy(Of T)(collection, orderByInfo)
Next
Return collection
End Function
Private Function ApplyOrderBy(Of T)(collection As IQueryable(Of T), orderByInfo As OrderByInfo) As IQueryable(Of T)
Dim props As String() = orderByInfo.PropertyName.Split("."c)
Dim type As Type = GetType(T)
Dim arg As ParameterExpression = Expression.Parameter(type, "x")
Dim expr As Expression = arg
For Each prop As String In props
' use reflection (not ComponentModel) to mirror LINQ
Dim pi As PropertyInfo = type.GetProperty(prop)
expr = Expression.[Property](expr, pi)
type = pi.PropertyType
Next
Dim delegateType As Type = GetType(Func(Of ,)).MakeGenericType(GetType(T), type)
Dim lambda As LambdaExpression = Expression.Lambda(delegateType, expr, arg)
Dim methodName As String = [String].Empty
If Not orderByInfo.Initial AndAlso TypeOf collection Is IOrderedQueryable(Of T) Then
If orderByInfo.Direction = SortDirection.Ascending Then
methodName = "ThenBy"
Else
methodName = "ThenByDescending"
End If
Else
If orderByInfo.Direction = SortDirection.Ascending Then
methodName = "OrderBy"
Else
methodName = "OrderByDescending"
End If
End If
'TODO: apply caching to the generic methodsinfos?
Return DirectCast(GetType(Queryable).GetMethods().[Single](Function(method) method.Name = methodName AndAlso method.IsGenericMethodDefinition AndAlso method.GetGenericArguments().Length = 2 AndAlso method.GetParameters().Length = 2).MakeGenericMethod(GetType(T), type).Invoke(Nothing, New Object() {collection, lambda}), IOrderedQueryable(Of T))
End Function
Private Function ParseOrderBy(orderBy As String) As IEnumerable(Of OrderByInfo)
If [String].IsNullOrEmpty(orderBy) Then
Return New List(Of OrderByInfo)
End If
Dim items As String() = orderBy.Split(","c)
Dim initial As Boolean = True
Dim listorderby As New List(Of OrderByInfo)
For Each item As String In items
Dim pair As String() = item.Trim().Split(" "c)
If pair.Length > 2 Then
Throw New ArgumentException([String].Format("Invalid OrderBy string '{0}'. Order By Format: Property, Property2 ASC, Property2 DESC", item))
End If
Dim prop As String = pair(0).Trim()
If [String].IsNullOrEmpty(prop) Then
Throw New ArgumentException("Invalid Property. Order By Format: Property, Property2 ASC, Property2 DESC")
End If
Dim dir As SortDirection = SortDirection.Ascending
If pair.Length = 2 Then
dir = (If("desc".Equals(pair(1).Trim(), StringComparison.OrdinalIgnoreCase), SortDirection.Descending, SortDirection.Ascending))
End If
Dim objclss = New OrderByInfo()
objclss.PropertyName = prop
objclss.Direction = dir
objclss.Initial = initial
listorderby.Add(objclss)
initial = False
Next
Return listorderby
End Function
Private Class OrderByInfo
Public Property PropertyName() As String
Get
Return m_PropertyName
End Get
Set(value As String)
m_PropertyName = value
End Set
End Property
Private m_PropertyName As String
Public Property Direction() As SortDirection
Get
Return m_Direction
End Get
Set(value As SortDirection)
m_Direction = value
End Set
End Property
Private m_Direction As SortDirection
Public Property Initial() As Boolean
Get
Return m_Initial
End Get
Set(value As Boolean)
m_Initial = value
End Set
End Property
Private m_Initial As Boolean
End Class
Private Enum SortDirection
Ascending = 0
Descending = 1
End Enum
End Module
------------------------------------------------
ไม่มีความคิดเห็น:
แสดงความคิดเห็น