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();
}
}
留待后查,同時(shí)方便他人
聯(lián)系我:renhanlinbsl@163.com
聯(lián)系我:renhanlinbsl@163.com

浙公網(wǎng)安備 33010602011771號(hào)