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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:12:07+00:00 2026-06-14T14:12:07+00:00

Given an Expression<Func<T, TValue>> (like m => m.Name ) and an index , I’d

  • 0

Given an Expression<Func<T, TValue>> (like m => m.Name) and an index, I’d like to be able to transform my expression to m => m[index].Name). And I must admit I’m stuck…

I give you the actual scenario if you want the “Why the hell” (and maybe find a better way).

Scenario : imagine a Server Side Editable Grid (without javascript).

I build my grid with an helper which look like that :

@(Html.Grid(Model)
.Columns(columns => {
   columns.Edit(m => m.Name);
   columns.Edit(m => m.Code);
})
.AsEditable());

Model is an IQueryable<T>

m => m.Name is an Expression<Func<T, TValue>> (TValue is string)

m => m.Code is an Expression<Func<T, TValue>> (TValue is int)

When rendering my view, I’d like to display an html form.
The IQueryable<T> is enumerated (order, pagination). => ok

So I’ll have a List<T> of 5, 10 or 20 T items.

And Name and Code should be represented as TextBox, using a classic HtmlHelper.TextBoxFor(Expression<Func<T, TValue>>) (no problem to create the HtmlHelper<T>)

But as I’ve got a list, if I want correct Model binding, I can’t use directly m => m.Name, but should use m => m[indexOfItem in List<T>].Name

Edit : more details :

So let’s say we have an entity class

public class Test {
  public int Id {get;set;}
  public string Name {get;set;}
  public string Code {get;set;}
}

Then, a method retrieving an IQueryable<Test>

Then a view

@model IQueryable<Test>

@(Html.Grid(Model)
    .Columns(columns => {
       columns.Edit(m => m.Name);
       columns.Edit(m => m.Code);
    })
    .AsEditable());

as you see, Model given as parameter is IQueryable<Test>.

m => m.Name

and

m => m.Code

are just properties of the Model (which I wanna display as TextBox in my grid).

The model is an IQueryable<T> (not an IEnumerable<T>), because the Grid manages ordering and Pagination, so that my controller and service layer don’t need to know about pagination and ordering.

Is it clearer ?

  • 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-14T14:12:08+00:00Added an answer on June 14, 2026 at 2:12 pm

    This could be easily achieved by writing a custom ExpressionVisitor:

    public static class ExpressionExtensions
    {
        private class Visitor : ExpressionVisitor
        {
            private readonly int _index;
            public Visitor(int index)
            {
                _index = index;
            }
    
            protected override Expression VisitParameter(ParameterExpression node)
            {
                return Expression.ArrayIndex(GetArrayParameter(node), Expression.Constant(_index));
            }
    
            public ParameterExpression GetArrayParameter(ParameterExpression parameter)
            {
                var arrayType = parameter.Type.MakeArrayType();
                return Expression.Parameter(arrayType, parameter.Name);
            }
        }
    
        public static Expression<Func<T[], TValue>> BuildArrayFromExpression<T, TValue>(
            this Expression<Func<T, TValue>> expression,
            int index
        )
        {
            var visitor = new Visitor(index);
            var nexExpression = visitor.Visit(expression.Body);
            var parameter = visitor.GetArrayParameter(expression.Parameters.Single());
            return Expression.Lambda<Func<T[], TValue>>(nexExpression, parameter);
        }
    }
    

    and then you could use this extension method like this:

    Expression<Func<Test, string>> ex = m => m.Code;
    Expression<Func<Test[], string>> newEx = ex.BuildArrayFromExpression(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a Lambda Expression: Define(Expression<Func<T, int>> property) and used like: Define(x => x.Collection.Count) What
Given an Expression<Func<T, object>> (e.g. x => x.Prop1.SubProp), I want to create a string
Given this code: int min = 0; Expression<Func<List<IUser>, bool>> ulContainsJohn = (l => l.Where(u
Given a; Expression<Func<T, bool>> criteria; Is it possible to convert that to an IMongoQuery
Given the following classes: public class Class1<TObject> { protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref
Given a method signature: public bool AreTheSame<T>(Expression<Func<T, object>> exp1, Expression<Func<T, object>> exp2) What would
Given a lambda that takes an Identification object, and returns a property: Expression<Func<Identification, object>>
Given the following Expression<Func<T,bool>> matchExpression; How can i create another expression that is a
Given a regular expression: /say (hullo|goodbye) to my lovely (.*)/ and a string: my
Problem Given a boolean expression consisting of the symbols 0, 1, &, |, ^

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.