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

  • SEARCH
  • Home
  • 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 8167783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:18:34+00:00 2026-06-06T20:18:34+00:00

I am trying to select a column from an IEnumerable collection that has a

  • 0

I am trying to select a column from an IEnumerable collection that has a type known only to me at runtime. The only way I can think of using this is using LINQ expressions to build a dynamic call to Queryable.Select. However, I’m having a lot of trouble figuring out the proper syntax to accomplish this.

An example of how I would do this in the happy-go-lucky world of knowing everything I need at compile time, my code would look like this:

' Create an IEnumerable(Of String)
Dim strings = { "one", "two", "three" }

' Produce a collection with elements {3,3,5}
Dim stringLengths = strings.Select(Function(x) x.Length)

Unfortunately, in reality I have no idea that the collection I have is of type String, or that the property I want to select is Length. What I do have is an IEnumerable collection of items, as well as a PropertyInfo of the column that I want to select which provides me all of the type information I need.

As far as expressions go, I’ve been able to create a LINQ expression that I believe would represent the lambda I would normally pass to select ( Assume I am trying to perform the same operation above, with String and String.Length)

' pi is the PropertyInfo containing the Length property I am trying to select.
' pi.DeclaringType is String and pi.Name is Length
Dim targetItem = Expression.Parameter(pi.DeclaringType, "x")
Dim targetProperty = Expression.Property(targetItem, pi.Name)

' Produces the lambda<Function(x) x.Length>
Dim selectLambda = Expression.Lambda(targetProperty, targetItem)

Now hopefully all that’s left is to build is the call to Queryable.Select. To me, the syntax of Expression.Call is pretty confusing to say the least. My attempt is as follows (which fails with no errors or explanation of any kind):

' Creates a Parameter Expression of type IQueryable(Of String)
Dim source = Expression.Parameter(GetType(IQueryable(Of )).MakeGenericType(pi.DeclaringType), "source")

' Ideally, this would create an expression for a call to Queryable.Select
Dim selectCall = Expression.Call(GetType(Queryable), "Select", {pi.DeclaringType, pi.PropertyType}, source)

I tried doing this in an alternative way without using the Type[] parameter and using the expressions for my item and property to no avail:

Dim alternateExp = Expression.Call(GetType(Queryable), "Select", Nothing, {targetProperty, item})

The problem is that I’m pretty much just guessing at this point. However, the whole idea of building the function call, when to use Types or Expressions, which types or expressions to use, or where to even use them is just plain confusing. Any help getting me the last part of the way there and clearing some of this mystery would be greatly appreciated. (I’m perfectly happy with examples in C#)

  • 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-06-06T20:18:36+00:00Added an answer on June 6, 2026 at 8:18 pm
     var propertyType = typeof (string);
     var propertyName = "Length";
     IEnumerable list = new ArrayList { "one", "two", "three" };
    
    
      var item = Expression.Parameter(typeof(object), "x");
      var cast = Expression.Convert(item, propertyType);
      var propertyValue = Expression.PropertyOrField(cast, propertyName);
      var propertyValueAsObject = Expression.Convert(propertyValue, typeof (object));
      var selectLambda = Expression.Lambda<Func<object, object>>(propertyValueAsObject, item);
    
      list.Cast<object>().AsQueryable().Select(selectLambda);
    

    This is an answer using expressions, basically we deal with everything as an Object (casting to our runtime type and then casting back to Object for the final result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so I'm trying to select an amount of rows from a column that
I am trying to select a column named YEAR from a table. I can
I'm trying to execute a SELECT statement that includes a column of a static
Im trying to select the title column from a particular row $eventid = $_GET['id'];
I’m trying to select various table and column names from a (SQL Server 2008)
I'm trying to select a column from a single table (no joins) and I
I'm trying to use SQL like select column from table where column in (?)
I m trying to select a column's value from a datatable based on conditions.
Hello I' trying to select from joined column: SELECT A.idAd, A.ads_in_Cat, A.title, A.currency, A.price,
I'm trying to use LINQ expressions to dynamically select columns from an IEnumerable into

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.