Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From your <a href="https://gist.github.com/3899759" rel="nofollow noreferrer">gist</a> below I have found a easy way t flatten the properties across all base types and interfaces as demonstrated on this <a href="https://stackoverflow.com/questions/358835/getproperties-to-return-all-properties-for-an-interface-inheritance-hierarchy">post</a>.</p> <p>So I implemented the extension method for PropertyInfo that will return all properties from all interfaces and base classes inherited by the type. The problem was that IList does not have a Count property however iCollection does. The <code>public static PropertyInfo[] GetPublicProperties(this Type type)</code> will flat all properties and we get the correct one from there this should work on any property now not only Count.</p> <pre><code>public class Program { private static IList&lt;Company&gt; _companies; static void Main(string[] args) { var sort = "Employees.Count"; _companies = new List&lt;Company&gt;(); _companies.Add(new Company { Name = "c2", Address = new Address {PostalCode = "456"}, Employees = new List&lt;Employee&gt; {new Employee(), new Employee()} }); _companies.Add(new Company { Name = "c1", Address = new Address {PostalCode = "123"}, Employees = new List&lt;Employee&gt; { new Employee(), new Employee(), new Employee() } }); //display companies _companies.AsQueryable().OrderBy(sort).ToList().ForEach(c =&gt; Console.WriteLine(c.Name)); Console.ReadLine(); } } public class Company { public string Name { get; set; } public Address Address { get; set; } public IList&lt;Employee&gt; Employees { get; set; } } public class Employee{} public class Address { public string PostalCode { get; set; } } public static class OrderByString { public static IOrderedQueryable&lt;T&gt; OrderBy&lt;T&gt;(this IQueryable&lt;T&gt; source, string property) { return ApplyOrder&lt;T&gt;(source, property, "OrderBy"); } public static IOrderedQueryable&lt;T&gt; ApplyOrder&lt;T&gt;(IQueryable&lt;T&gt; 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.GetPublicProperties().FirstOrDefault(c =&gt; c.Name == prop); if (pi != null) { expr = Expression.Property(expr, pi); type = pi.PropertyType; } else { throw new ArgumentNullException(); } } Type delegateType = typeof(Func&lt;,&gt;).MakeGenericType(typeof(T), type); LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg); object result = typeof(Queryable).GetMethods().Single( method =&gt; method.Name == methodName &amp;&amp; method.IsGenericMethodDefinition &amp;&amp; method.GetGenericArguments().Length == 2 &amp;&amp; method.GetParameters().Length == 2) .MakeGenericMethod(typeof(T), type) .Invoke(null, new object[] { source, lambda }); return (IOrderedQueryable&lt;T&gt;)result; } public static PropertyInfo[] GetPublicProperties(this Type type) { if (type.IsInterface) { var propertyInfos = new List&lt;PropertyInfo&gt;(); var considered = new List&lt;Type&gt;(); var queue = new Queue&lt;Type&gt;(); considered.Add(type); queue.Enqueue(type); while (queue.Count &gt; 0) { var subType = queue.Dequeue(); foreach (var subInterface in subType.GetInterfaces()) { if (considered.Contains(subInterface)) continue; considered.Add(subInterface); queue.Enqueue(subInterface); } var typeProperties = subType.GetProperties( BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance); var newPropertyInfos = typeProperties .Where(x =&gt; !propertyInfos.Contains(x)); propertyInfos.InsertRange(0, newPropertyInfos); } return propertyInfos.ToArray(); } return type.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload