Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 68543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:25:59+00:00 2026-05-10T19:25:59+00:00

I found an example in the VS2008 Examples for Dynamic LINQ that allows you

  • 0

I found an example in the VS2008 Examples for Dynamic LINQ that allows you to use a SQL-like string (e.g. OrderBy("Name, Age DESC")) for ordering. Unfortunately, the method included only works on IQueryable<T>. Is there any way to get this functionality on IEnumerable<T>?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:25:59+00:00Added an answer on May 10, 2026 at 7:25 pm

    Just stumbled into this oldie…

    To do this without the dynamic LINQ library, you just need the code as below. This covers most common scenarios including nested properties.

    To get it working with IEnumerable<T> you could add some wrapper methods that go via AsQueryable – but the code below is the core Expression logic needed.

    public static IOrderedQueryable<T> OrderBy<T>(     this IQueryable<T> source,      string property) {     return ApplyOrder<T>(source, property, 'OrderBy'); }  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; } 

    Edit: it gets more fun if you want to mix that with dynamic – although note that dynamic only applies to LINQ-to-Objects (expression-trees for ORMs etc can’t really represent dynamic queries – MemberExpression doesn’t support it). But here’s a way to do it with LINQ-to-Objects. Note that the choice of Hashtable is due to favorable locking semantics:

    using Microsoft.CSharp.RuntimeBinder; using System; using System.Collections; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Runtime.CompilerServices; static class Program {     private static class AccessorCache     {         private static readonly Hashtable accessors = new Hashtable();          private static readonly Hashtable callSites = new Hashtable();          private static CallSite<Func<CallSite, object, object>> GetCallSiteLocked(             string name)          {             var callSite = (CallSite<Func<CallSite, object, object>>)callSites[name];             if(callSite == null)             {                 callSites[name] = callSite = CallSite<Func<CallSite, object, object>>                     .Create(Binder.GetMember(                                 CSharpBinderFlags.None,                                  name,                                  typeof(AccessorCache),                                 new CSharpArgumentInfo[] {                                      CSharpArgumentInfo.Create(                                         CSharpArgumentInfoFlags.None,                                          null)                                  }));             }             return callSite;         }          internal static Func<dynamic,object> GetAccessor(string name)         {             Func<dynamic, object> accessor = (Func<dynamic, object>)accessors[name];             if (accessor == null)             {                 lock (accessors )                 {                     accessor = (Func<dynamic, object>)accessors[name];                     if (accessor == null)                     {                         if(name.IndexOf('.') >= 0) {                             string[] props = name.Split('.');                             CallSite<Func<CallSite, object, object>>[] arr                                  = Array.ConvertAll(props, GetCallSiteLocked);                             accessor = target =>                             {                                 object val = (object)target;                                 for (int i = 0; i < arr.Length; i++)                                 {                                     var cs = arr[i];                                     val = cs.Target(cs, val);                                 }                                 return val;                             };                         } else {                             var callSite = GetCallSiteLocked(name);                             accessor = target =>                             {                                 return callSite.Target(callSite, (object)target);                             };                         }                         accessors[name] = accessor;                     }                 }             }             return accessor;         }     }      public static IOrderedEnumerable<dynamic> OrderBy(         this IEnumerable<dynamic> source,          string property)     {         return Enumerable.OrderBy<dynamic, object>(             source,              AccessorCache.GetAccessor(property),              Comparer<object>.Default);     }      public static IOrderedEnumerable<dynamic> OrderByDescending(         this IEnumerable<dynamic> source,          string property)     {         return Enumerable.OrderByDescending<dynamic, object>(             source,              AccessorCache.GetAccessor(property),              Comparer<object>.Default);     }      public static IOrderedEnumerable<dynamic> ThenBy(         this IOrderedEnumerable<dynamic> source,          string property)     {         return Enumerable.ThenBy<dynamic, object>(             source,              AccessorCache.GetAccessor(property),              Comparer<object>.Default);     }      public static IOrderedEnumerable<dynamic> ThenByDescending(         this IOrderedEnumerable<dynamic> source,          string property)     {         return Enumerable.ThenByDescending<dynamic, object>(             source,              AccessorCache.GetAccessor(property),              Comparer<object>.Default);     }      static void Main()     {         dynamic a = new ExpandoObject(),                  b = new ExpandoObject(),                  c = new ExpandoObject();         a.X = 'abc';         b.X = 'ghi';         c.X = 'def';         dynamic[] data = new[] {              new { Y = a },             new { Y = b },              new { Y = c }          };          var ordered = data.OrderByDescending('Y.X').ToArray();         foreach (var obj in ordered)         {             Console.WriteLine(obj.Y.X);         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 71k
  • Answers 71k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can decide yourself after reading the topic 'Planning Your… May 11, 2026 at 1:11 pm
  • added an answer You are correct in that the problem is with your… May 11, 2026 at 1:11 pm
  • added an answer You seem to be thinking that the exception is raised… May 11, 2026 at 1:11 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.