ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

c# Listdynamic 按字段排序

c# Listdynamic 按字段排序
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();}}
返回列表