c# List<dynamic> 按字段排序
public static List<dynamic> OrderByKey (this IList<dynamic> list, string propertyName, bool isDescending = false)
{
var propertyInfo = list[0].GetType().GetProperty(propertyName);
if (isDescending)
{
return list.OrderByDescending(x => propertyInfo.GetValue(x, null)).ToList();
}
else
{
return list.OrderBy(x => propertyInfo.GetValue(x, null)).ToList();
}
}
public static List<T> OrderByKey<T>(this IList<T> list, string propertyName, bool isDescending = false)
{
var propertyInfo = typeof(T).GetProperty(propertyName);
if (isDescending)
{
return list.OrderByDescending(x => propertyInfo.GetValue(x, null)).ToList();
}
else
{
return list.OrderBy(x => propertyInfo.GetValue(x, null)).ToList();
}
}
留待后查,同时方便他人
联系我:renhanlinbsl@163.com
联系我:renhanlinbsl@163.com