Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

Expressions

By: RichardD on Oct 4th, 2011  |  syntax: C#  |  size: 3.61 KB  |  views: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. internal static class Extensions
  2. {
  3.     public static IEnumerable<string> GetPrimaryKeyForType(this Type elementType)
  4.     {
  5.         return from PropertyDescriptor property in TypeDescriptor.GetProperties(elementType)
  6.                where null != property.Attributes[typeof(KeyAttribute)]
  7.                select property.Name;
  8.     }
  9.    
  10.     private static IOrderedQueryable SortCore(IQueryable source, string propertyName, ListSortDirection sortDirection, string methodName)
  11.     {
  12.         if (ListSortDirection.Descending == sortDirection)
  13.         {
  14.             methodName += "Descending";
  15.         }
  16.  
  17.         var p = Expression.Parameter(source.ElementType, "p");
  18.         var prop = Expression.Property(p, propertyName);
  19.         var body = Expression.Lambda(prop, p);
  20.  
  21.         var call = Expression.Call(typeof(Queryable), methodName,
  22.             new[] { p.Type, prop.Type },
  23.             source.Expression, Expression.Quote(body));
  24.  
  25.         return (IOrderedQueryable)source.Provider.CreateQuery(call);
  26.     }
  27.    
  28.     public static IOrderedQueryable Sort(this IQueryable source, IEnumerable<Tuple<string, ListSortDirection>> sortBy)
  29.     {
  30.         if (null == source) throw new ArgumentNullException("source");
  31.         if (null == sortBy) throw new ArgumentNullException("sortBy");
  32.  
  33.         IOrderedQueryable result;
  34.         using (var enumerator = sortBy.Where(t => null != t).GetEnumerator())
  35.         {
  36.             if (!enumerator.MoveNext())
  37.             {
  38.                 throw new ArgumentException(Resources.Error_EmptyEnumerable, "sortBy");
  39.             }
  40.  
  41.             var tuple = enumerator.Current;
  42.             result = SortCore(source, tuple.Item1, tuple.Item2, "OrderBy");
  43.             while (enumerator.MoveNext())
  44.             {
  45.                 tuple = enumerator.Current;
  46.                 result = SortCore(result, tuple.Item1, tuple.Item2, "ThenBy");
  47.             }
  48.         }
  49.  
  50.         return result;
  51.     }
  52.    
  53.     public static long LongCount(this IQueryable source)
  54.     {
  55.         if (null == source) throw new ArgumentNullException("source");
  56.  
  57.         Expression call = Expression.Call(typeof(Queryable), "LongCount",
  58.             new[] { source.ElementType },
  59.             source.Expression);
  60.  
  61.         return source.Provider.Execute<long>(call);
  62.     }
  63.    
  64.     public static IQueryable Take(this IQueryable source, int count)
  65.     {
  66.         if (null == source) throw new ArgumentNullException("source");
  67.  
  68.         var call = Expression.Call(typeof(Queryable), "Take",
  69.             new[] { source.ElementType },
  70.             source.Expression, Expression.Constant(count));
  71.  
  72.         return source.Provider.CreateQuery(call);
  73.     }
  74.    
  75.     public static IQueryable SkipLong(this IQueryable source, long count)
  76.     {
  77.         if (null == source) throw new ArgumentNullException("source");
  78.  
  79.         while (0L < count)
  80.         {
  81.             int toSkip;
  82.             if (int.MaxValue < count)
  83.             {
  84.                 count -= int.MaxValue;
  85.                 toSkip = int.MaxValue;
  86.             }
  87.             else
  88.             {
  89.                 toSkip = (int)count;
  90.                 count = 0L;
  91.             }
  92.  
  93.             var call = Expression.Call(typeof(Queryable), "Skip",
  94.                 new[] { source.ElementType },
  95.                 source.Expression, Expression.Constant(toSkip));
  96.  
  97.             source = source.Provider.CreateQuery(call);
  98.         }
  99.  
  100.         return source;
  101.     }
  102.    
  103.     public static IEnumerable Materialize(this IQueryable source)
  104.     {
  105.         if (null == source) throw new ArgumentNullException("source");
  106.         return Enumerable.Cast<object>(source).ToList();
  107.     }
  108. }