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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:56:05+00:00 2026-06-11T15:56:05+00:00

I have a lambda expression tree issue that I can’t figure out. I am

  • 0

I have a lambda expression tree issue that I can’t figure out. I am trying to make a dynamic linq Select statement.

I have a dynamic repository created here:

private static dynamic GetRepository(Type type)
{
    dynamic repository = typeof(IFactory).GetMethod("Create").MakeGenericMethod(typeof(IRepository<>).MakeGenericType(type)).Invoke(ObjectFactory.Instance, new object[] { });
    return repository;
}

With this I need to call this only I don’t know x and SomeProperty at compile time. I have PropertyInfo propertyInfo with SomeProperty name and Type objectType with x type.
It fails at Goal 1 with this exception:

System.Reflection.AmbiguousMatchException at GetMethod(string name)

The code:

private SomeObject CreateSomeObject (PropertyInfo propertyInfo, Type objectType)
{
    var param = Expression.Parameter(objectType, "x");
    MemberExpression expression = Expression.PropertyOrField(param, propertyInfo.Name);

    //Goal 1: var selectExpression = Expression.Lambda<Func<objectType, object>>(expression, param);
    var selectExpression = typeof(Expression).GetMethod("Lambda").MakeGenericMethod(typeof(Func<,>)
    .MakeGenericType(objectType, typeof(object)))
    .Invoke((object)null, new object[] { expression, param });

    // Goal 2: List<object> list = GetRepository(objectType).FindAllQuery().Select(x => x.SomeProperty).ToList();
    List<object> list = GetRepository(objectType).FindAll().Select(selectExpression);
}

How to solve this?

Update 1:

I have changed the way to select Lambda method, the way to pack ‘param’ parameter and I have added a object Converter to ‘expression’.

private SomeObject CreateSomeObject (PropertyInfo propertyInfo, Type objectType)
{
    var param = Expression.Parameter(objectType, "x");
    Expression expression = Expression.Convert(Expression.PropertyOrField(param, propertyInfo.Name), typeof(object));

    //Goal 1: var selectExpression = Expression.Lambda<Func<objectType, object>>(expression, param);
    var selectExpression = typeof(Expression).GetMethods().First(m => m.Name == "Lambda" && m.IsGenericMethod)
    .MakeGenericMethod(typeof(Func<,>)
    .MakeGenericType(objectType, typeof(object)))
    .Invoke((object)null, new object[] { expression, new [] { param }});

    // Goal 2: List<object> list = GetRepository(objectType).FindAllQuery().Select(x => x.SomeProperty).ToList();
    List<object> list = GetRepository(objectType).FindAll().Select(selectExpression);
}

But know I get this exception at Goal 2(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException):

‘System.Collections.Generic.List ‘ does not contain a
definition for ‘Select’

This is partly right because it is defined in System.Linq and it is an extension method. How do I get this working?

  • 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-11T15:56:06+00:00Added an answer on June 11, 2026 at 3:56 pm

    The code that throws the exception is

    typeof(Expression).GetMethod("Lambda")
    

    because there are 18 methods called Lambda defined on the Expression type (thus the AmbiguousMatchException).

    GetMethod(string methodName) is appropriate when there are no overloads. In this case I would use GetMethods() and then filter out the one I need.

    In your case, the right overload is

    Expression.Lambda<TDelegate>(Expression body, params ParameterExpression[] parameters)
    

    You could write a function that validates the right overload by checking the number of parameters and their type, but I found an easier alternative: filter the method by the .ToString() representation, which in our case is:

    System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate](System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])
    

    There’s also a problem with the way you pass the parameters (new object[] { expression, param }). The second parameter is not of type ParameterExpression, but ParameterExpression[] (array), therefore you should pass new[]{param} instead of just param. When calling it in regular code it works like that because it’s defined as params ParameterExpression[].

    In conclusion, the following code should work in your case:

    const string methodSignature = 
        "System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate]" +
        "(System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])";
    
    var lambdaMethod = typeof (Expression).GetMethods()
        .Single(mi => mi.ToString() == methodSignature);
    
    var funcType = typeof (Func<,>).MakeGenericType(objectType, typeof (object));
    
    var genericLambda = lambdaMethod.MakeGenericMethod(funcType);
    
    var selectExpression = genericLambda.Invoke(null, new object[] { expression, new[] { param } });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree expression that looks like this: .Block( System.Object $instance, MyType2 $result)
I have an inline lambda expression that I would like to use throughout my
I want to build a Lambda Expression using Linq Expressions that is able to
I am trying to build an expression tree programmatically. I have in my input,
I have a lambda expression that gets results from a Dictionary. var sortedDict =
I am very confused. I have this lambda expression: tvPatientPrecriptionsEntities.Sort((p1, p2) => p1.MedicationStartDate .Value
I am trying to build a lambda expression, containing two assignments (as shown further
I have a method which have this signature public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
I have the following lambda expression: IEnumerable<Order> query = _ordersRepository.GetAllByFilter( o => o.OrderStatus.OrderByDescending(os =>
I'm using the standard visitor pattern to iterate through a LINQ expression tree in

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.