构建LINQ动态顺序子句,但强制转换字段
以下代码非常有用,因为我想动态构建orderby:构建LINQ动态顺序子句,但强制转换字段,linq,dynamic,expression,Linq,Dynamic,Expression,以下代码非常有用,因为我想动态构建orderby: public static IQueryable<TEntity> OrderByAnyField<TEntity>(this IQueryable<TEntity> source, string orderByProperty, bool desc, Type propertyType) { string command = desc ? "OrderByDescending
public static IQueryable<TEntity> OrderByAnyField<TEntity>(this IQueryable<TEntity> source, string orderByProperty, bool desc, Type propertyType)
{
string command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);
var property = type.GetProperty(orderByProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExpression));
return source.Provider.CreateQuery<TEntity>(resultExpression);
}
而是这样做:
.OrderBy(x => double.Parse(x.Something))
非常感谢您的帮助我正在分享一种更简单的方法。您可以根据需要添加泛型。您可以以任何方式播放数据
static object GetOrder(Table tb, string propertyName, bool desc)
{
if (desc)
return 0;
PropertyInfo pI = typeof(Table).GetProperty(propertyName);
var val = pI.GetValue(tb);
return val;
}
static object GetOrderDesc(Table tb, string propertyName, bool desc)
{
if (!desc)
return 0;
PropertyInfo pI = typeof(Table).GetProperty(propertyName);
var val = pI.GetValue(tb);
return val;
}
static void Main(string[] args)
{
bool desc = false;
List<Table> table = new List<Table>() {
new Table() { ID = "03", X = "Str1", Y = "C1", P = 10 },
new Table() { ID = "04", X = "Str1", Y = "C1", P = 5 },
new Table() { ID = "05", X = "Str1", Y = "C1", P = 1 },
new Table() { ID = "06", X = "Str1", Y = "C1", P = 2 },
new Table() { ID = "07", X = "Str2", Y = "C1", P = 25 },
new Table() { ID = "08", X = "Str2", Y = "C1", P = 4 },
new Table() { ID = "09", X = "Str1", Y = "C2", P = 411 },
new Table() { ID = "10", X = "Str1", Y = "C2", P = 2356 },
new Table() { ID = "11", X = "Str2", Y = "C2", P = 12 },
new Table() { ID = "12", X = "Str2", Y = "C2", P = 33 },
};
var sortedTable = table.OrderBy(x => GetOrder(x, "P", desc)).OrderByDescending(x => GetOrderDesc(x, "P", desc));
}
静态对象GetOrder(表tb、字符串propertyName、bool desc)
{
如果(描述)
返回0;
PropertyInfo pI=typeof(Table).GetProperty(propertyName);
var val=pI.GetValue(tb);
返回val;
}
静态对象GetOrderDesc(表tb、字符串propertyName、bool desc)
{
如果(!desc)
返回0;
PropertyInfo pI=typeof(Table).GetProperty(propertyName);
var val=pI.GetValue(tb);
返回val;
}
静态void Main(字符串[]参数)
{
bool desc=假;
列表=新列表(){
新表(){ID=“03”,X=“Str1”,Y=“C1”,P=10},
新表(){ID=“04”,X=“Str1”,Y=“C1”,P=5},
新表(){ID=“05”,X=“Str1”,Y=“C1”,P=1},
新表(){ID=“06”,X=“Str1”,Y=“C1”,P=2},
新表(){ID=“07”,X=“Str2”,Y=“C1”,P=25},
新表(){ID=“08”,X=“Str2”,Y=“C1”,P=4},
新表(){ID=“09”,X=“Str1”,Y=“C2”,P=411},
新表(){ID=“10”,X=“Str1”,Y=“C2”,P=2356},
新表(){ID=“11”,X=“Str2”,Y=“C2”,P=12},
新表(){ID=“12”,X=“Str2”,Y=“C2”,P=33},
};
var sortedTable=table.OrderBy(x=>GetOrder(x,“P”,desc)).OrderByDescending(x=>GetOrderDesc(x,“P”,desc));
}
当linq已经为您提供了对任何属性进行排序的功能时,您在这里想要实现什么?我需要根据传递的列名动态排序。我还需要“铸造”专栏。因此,我使用上面的代码按传递的列名进行排序。(即,OrderByAnyField(passedColumnName,false,typeof(double)))。passedColumnName可以是源中任何列的名称。为了清晰起见,让我添加到我的注释中。我可以按列排序,与.OrderBy(x=>x.Something)相同。我想通过强制转换到不同的数据类型进行排序,比如.OrderBy(x=>double.Parse(x.Something))。如果我尝试使用不同的数据类型,在resultExpression中,我会得到一个错误:
static object GetOrder(Table tb, string propertyName, bool desc)
{
if (desc)
return 0;
PropertyInfo pI = typeof(Table).GetProperty(propertyName);
var val = pI.GetValue(tb);
return val;
}
static object GetOrderDesc(Table tb, string propertyName, bool desc)
{
if (!desc)
return 0;
PropertyInfo pI = typeof(Table).GetProperty(propertyName);
var val = pI.GetValue(tb);
return val;
}
static void Main(string[] args)
{
bool desc = false;
List<Table> table = new List<Table>() {
new Table() { ID = "03", X = "Str1", Y = "C1", P = 10 },
new Table() { ID = "04", X = "Str1", Y = "C1", P = 5 },
new Table() { ID = "05", X = "Str1", Y = "C1", P = 1 },
new Table() { ID = "06", X = "Str1", Y = "C1", P = 2 },
new Table() { ID = "07", X = "Str2", Y = "C1", P = 25 },
new Table() { ID = "08", X = "Str2", Y = "C1", P = 4 },
new Table() { ID = "09", X = "Str1", Y = "C2", P = 411 },
new Table() { ID = "10", X = "Str1", Y = "C2", P = 2356 },
new Table() { ID = "11", X = "Str2", Y = "C2", P = 12 },
new Table() { ID = "12", X = "Str2", Y = "C2", P = 33 },
};
var sortedTable = table.OrderBy(x => GetOrder(x, "P", desc)).OrderByDescending(x => GetOrderDesc(x, "P", desc));
}