I built a VB.NET code to sort several type like string,number ... Now I try to Had date.
If (TypeClass.GetProperties().Any(Function(prop) prop.Name = sortPropertyName AndAlso prop.CanRead)) Then
'Information sur la propriété recherchée
Dim pinfo As PropertyInfo = TypeClass.GetProperty(sortPropertyName)
Dim Typ = pinfo.PropertyType.Name
Dim toStr As Expression
Dim Expr As Expression = Expression.Property(paramExpr, pinfo)
toStr = Expression.Call(If(pinfo.PropertyType.IsValueType, Expr, Expression.Coalesce(Expr, Expression.Constant(String.Empty))), "ToString", Nothing)
Dim orderByFunc As Func(Of MaClass, Object) = Expression.Lambda(Of Func(Of MaClass, Object))(toStr, paramExpr).Compile()
Dim sortFunc As Func(Of IEnumerable(Of MaClass), IOrderedEnumerable(Of MaClass)) = Nothing
If (Not CBool(Sort.Sens)) Then
sortFunc = (Function(source) source.OrderBy(orderByFunc))
Else
sortFunc = (Function(source) source.OrderByDescending(orderByFunc))
End If
query = sortFunc(query).ToList()
End If
The problem is when I sort it's not sort Date but a string like
31/12/2005; 31/11/2011; 31/10/2007 ...
When I Sort it's better to find
31/11/2011; 31/10/2007; 31/12/2005
Then I try this modify
If Typ.Contains("DateTime") Then 'Add For DateTime here
toStr = Expression.Call(If(pinfo.PropertyType.IsValueType, Expr, Expression.Coalesce(Expr, Expression.Constant(Date.MinValue))), "ToString", Nothing)
Else
toStr = Expression.Call(If(pinfo.PropertyType.IsValueType, Expr, Expression.Coalesce(Expr, Expression.Constant(String.Empty))), "ToString", Nothing)
End If
but i don't know how replace 'ToString'
I try
toStr = Expression.Call(If(pinfo.PropertyType.IsValueType, Expr, Expression.Coalesce(Expr, Expression.Constant(Date.MinValue))), "ToString(""yyyy MM dd"")", Nothing)
But I was following error
ex = {"Aucune méthode 'ToString("yyyy MM dd")' n'existe sur le type 'System.Nullable`1[System.DateTime]'."}
Translate by Google
"No method 'ToString (" yyyy dd MM ")' exists on the type 'System.Nullable`1 [System.DateTime]'.
I try too Ticks, Date or Year,,Value.Ticks, GetValueOrDefault.Year.ToString but same error
Perhaps there is a better way
Thnks for your help