I get the following string expression provided:
“ChildObject.FullName” …where ChildObject is an instance property on the MyObject1 type.
ChildObject has a property named “FullName” and I want to sort a collection of type “MyObject1” based on this child properties “FullName” value.
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:
- MyObject1 has a few different child property types so I can’t hardcode the type for ChildObject. The string could be of any type.
- The sort expression is a String and not a known type.
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:
'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()
Above the value returned from the last line in the expression (if I run the code outsode the OrderBy method, does provide me the ‘FullName’ information I need. So the code must be close, but it still does not work.
Any ideas on how I can accomplish this? What I am trying to prevent is hardcoding a series of ‘If’ blocks on the child’s type to then hardcode in its type to the sort or OrderBy method.
Thanks!
If I understand this correctly you have an object (say, Parent) which contains an object (say, Child). Child has a field FullName and you want to sort a list of parents by the child FullName.
If so, then then OrderBy() should do it for you.
Say we have a list of parents
Parent{ Id = 1, Child = { Id = 1, FullName = “Jan”}}
Parent{ Id = 2, Child = { Id = 2, FullName = “Feb”}}
Parent{ Id = 3, Child = { Id = 3, FullName = “Mar”}}
Parent{ Id = 4, Child = { Id = 4, FullName = “Apr”}}
sorting them using OrderBy()
gives
Parent{ Id = 4, Child = { Id = 4, FullName = “Apr”}}
Parent{ Id = 2, Child = { Id = 2, FullName = “Feb”}}
Parent{ Id = 1, Child = { Id = 1, FullName = “Jan”}}
Parent{ Id = 3, Child = { Id = 3, FullName = “Mar”}}
(example below)
hth,
Alan.
Edit
Just re-read the question. You have different FullName properties which are selected by name (from a query string?)
You can select the property by name using an expression (see, How can I create a dynamic Select on an IEnumerable<T> at runtime?) for the general shape.
If the selected property is IComparable (Or is it IEquatable? Not too sure which) then the OrderBy() will still work. This means that as long as the sort fields are basic types you are fine. If they are custom types (objects) you will need to do some more work…
Sorry about the first mis-answer.
More Edits
It’s Friday and slow in here 😕
OK, expanded the answer to access different child memebers by name. (I used Fields rather than Properties but either will work). We still need to know the type of the field but a bit more work might remove that (if needed).