最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# linq查詢之動(dòng)態(tài)OrderBy用法實(shí)例

 更新時(shí)間:2015年06月30日 11:43:21   作者:xtechnet  
這篇文章主要介紹了C# linq查詢之動(dòng)態(tài)OrderBy用法,實(shí)例分析了C#采用linq方式查詢時(shí)動(dòng)態(tài)排序的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了C# linq查詢之動(dòng)態(tài)OrderBy用法。分享給大家供大家參考。具體分析如下:

groupList是原始數(shù)據(jù)集合,List<T>

sortOrder是排序類型,desc 或者asc

sortName是排序?qū)傩悦Q

1.使用反射。

private static object GetPropertyValue(object obj, string property)
{
  System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
  return propertyInfo.GetValue(obj, null);
} 
var resultList = sortOrder == "desc" ? groupList.OrderByDescending(p => GetPropertyValue(p, sortName)) : groupList.OrderBy(p => GetPropertyValue(p, sortName));
//linq方式:
//
var resultList1 = from p in groupList orderby GetPropertyValue(p, m.SortName) select p;
if (sortOrder == "desc")
 resultList1 = from p in groupList orderby GetPropertyValue(p, sortName) descending select p;

2.調(diào)用AsQueryable()

將泛型 System.Collections.Generic.IEnumerable<T> 轉(zhuǎn)換為泛型 System.Linq.IQueryable<T>。

var groupQueryList = groupList.AsQueryable();//here
var tmpList = groupQueryList.OrderBy(sortName, sortOrder);

需要如下擴(kuò)展方法:

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) {
  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;
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

正镶白旗| 阜新市| 永济市| 承德市| 蓬莱市| 晴隆县| 庆阳市| 离岛区| 神农架林区| 永定县| 永定县| 乌兰县| 上犹县| 疏勒县| 紫阳县| 宣城市| 南平市| 鄂尔多斯市| 霍邱县| 肇庆市| 通海县| 绥棱县| 江西省| 广灵县| 乌鲁木齐市| 芜湖市| 秦皇岛市| 佳木斯市| 克什克腾旗| 汽车| 砚山县| 梓潼县| 商河县| 菏泽市| 临猗县| 广东省| 沾化县| 侯马市| 太康县| 金堂县| 舒兰市|