Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I order a collection based on a child property using LINQ or a Lambda?
    primarykey
    data
    text
    <p>I get the following <strong>string</strong> expression provided:</p> <p>"ChildObject.FullName" ...where ChildObject is an instance property on the MyObject1 type.</p> <p>ChildObject has a property named "FullName" and I want to sort a collection of type "MyObject1" based on this child properties "FullName" value.</p> <p>I can do this all day long on properties directly on MyObject1 but I run into 2 challanges when doing it on a child instance and I can't get all the pieces working. The main 2 challanges are:</p> <ol> <li>MyObject1 has a few different child property types so I can't hardcode the type for ChildObject. The <em>string</em> could be of any type.</li> <li>The sort expression is a String and not a known type.</li> </ol> <p>For #2 above I can use Reflection to get the type of the child property, but I just can't get it all to work. I have the following below and it compiles and runs, but does not sort any differently:</p> <pre><code>'SortExpression below is a String like: "ChildObject.FullName" MyObject1List = MyObject1List.OrderBy(Function(x) Dim t As Type = x.GetType() Dim tp As Type = t.GetProperty(SortExpression.Split(".").ElementAt(0)).PropertyType() Return tp.GetProperty(Request.CompareExpression.Split(".").ElementAt(1)) End Function).ToList() </code></pre> <p>Above the value returned from the last line in the expression (if I run the code outsode the OrderBy method, <em>does</em> provide me the 'FullName' information I need. So the code must be close, but it still does not work.</p> <p>Any ideas on how I can accomplish this? What I am trying to prevent is <strong>hardcoding</strong> a series of 'If' blocks on the child's type to then hardcode in its type to the sort or OrderBy method. </p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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