我在应用程序中使用以下代码对列表进行排序,并且工作正常。
public static class OrderByHelper
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
property = property.Trim();
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
我用
来称呼它 model.ProfileList = Helper.OrderByHelper.OrderBy(model.ProfileList.Cast<ProfileData>().AsQueryable(), ResultSet.SortField).ToList<ProfileData>();
现在我需要添加的是Collation,因为我在内存中加载列表所以使用数据库排序规则在这里没有任何意义。那么无论如何都要在代码中提供整理。可能吗?我谷歌但它还没找到。提前谢谢。
例如: 这里是挪威字符,应该在列表的最后添加,但在默认排序规则中它被视为A
åtestå
cxvfg
Design
答案 0 :(得分:0)
这是我的解决方案,未来可能有助于某人。这是使用culture对内存列表进行排序的通用方法。
public class MyStringComparer : IComparer<string>
{
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("no");
public int Compare(string x, string y)
{
return string.Compare(x, y, true, culture);
}
}
public static class OrderByHelper
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
property = property.Trim();
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
bool IsString = type.FullName == "System.String";
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
MyStringComparer comparer = new MyStringComparer();
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& (IsString ? method.GetParameters().Length == 3 : method.GetParameters().Length == 2))
.MakeGenericMethod(typeof(T), type)
.Invoke(null, IsString ? new object[] { source, lambda, comparer } : new object[] { source, lambda });
return ((IOrderedQueryable<T>)result);
}
}