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 7178061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:50:36+00:00 2026-05-28T16:50:36+00:00

I get the following string expression provided: ChildObject.FullName …where ChildObject is an instance property

  • 0

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:

  1. MyObject1 has a few different child property types so I can’t hardcode the type for ChildObject. The string could be of any type.
  2. 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!

  • 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. Editorial Team
    Editorial Team
    2026-05-28T16:50:38+00:00Added an answer on May 28, 2026 at 4:50 pm

    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()

    Dim sorted = Items.OrderBy(Function(itm) itm.Child.FullName)
    

    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).

    Private Class Child
    Public Id As Integer
    Public FullName As String
    End Class
    
    Private Class Parent
    Public Id As Integer
    Public Child As Child
    End Class
    
    Private Items As New List(Of Parent)() From { _
    New Parent() With { _
        Key .Id = 1, _
        Key .Child = New Child() With { _
            Key .Id = 1, _
            Key .FullName = "Jan" _
        } _
    }, _
    New Parent() With { _
        Key .Id = 2, _
        Key .Child = New Child() With { _
            Key .Id = 2, _
            Key .FullName = "Feb" _
        } _
    }, _
    New Parent() With { _
        Key .Id = 3, _
        Key .Child = New Child() With { _
            Key .Id = 3, _
            Key .FullName = "Mar" _
        } _
    }, _
    New Parent() With { _
        Key .Id = 4, _
        Key .Child = New Child() With { _
            Key .Id = 4, _
            Key .FullName = "Apr" _
        } _
    } _
    }
    
    <TestMethod> _
    Public Sub SortByChildName()
    Dim expectedParentIds = New () {4, 2, 1, 3}
    Dim sortedIds = Items.OrderBy(SelectExpression(Of Parent, String)("Child.FullName")).[Select](Function(itm) itm.Id)
    
    Assert.IsTrue(expectedParentIds.SequenceEqual(sortedIds))
    End Sub
    
    <TestMethod> _
    Public Sub SortByChildId()
    
    Dim expectedParentIds = New () {4, 3, 2, 1}
    Dim sortedIds = Items.OrderBy(SelectExpression(Of Parent, Integer)("Child.Id")).[Select](Function(itm) itm.Id)
    
    Assert.IsTrue(expectedParentIds.SequenceEqual(sortedIds))
    
    End Sub
    
    
    Public Shared Function SelectExpression(Of TItem, TField)(fieldNames As String) As Func(Of TItem, TField)
    
    Dim type = GetType(TItem)
    Dim fields = fieldNames.Split("."C)
    
    Dim arg As ParameterExpression = Expression.Parameter(type, "item")
    Dim expr As Expression = arg
    
    For Each field As String In fields
        Dim fieldInfo = type.GetField(field)
        expr = Expression.Field(expr, fieldInfo)
        type = fieldInfo.FieldType
    Next
    
    Return Expression.Lambda(Of Func(Of TItem, TField))(expr, arg).Compile()
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following property: public string Foo { get; private set; } Using reflection,
How to get the numeric value from the following string using regular expression AD
I' trying to get the following string to output to a cell in Excel
I'm trying to get the following code working: string url = String.Format(@SOMEURL); string user
I get the following text as a string from an XML-based REST API 'd':4
I get the following warning when using java.net.URLEncoder.encode : warning: [deprecation] encode(java.lang.String) in java.net.URLEncoder
I have the following code : std::string Utils::get() { std::string result; result.append(1, 'x'); result.append(1,
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
Given the following class definitions: public class BaseClass { public string SomeProp1 { get;
Consider the following class hierarchy: public class Foo { public string Name { get;

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.