Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
I can't seem to be able to access a Dictonary<string, object> values while building a dynamic linq expression

I'm trying to generate a linq expression in asp.net core for MongoDB using the MongoDriver and I can't access the runtime value of a dictonary with the expression generator. Thanks in advance!

private Expression<Func<Dictionary<string, object>, bool>> GenerateWhereExpression(Dictionary<string, object> filterParams)
{
    var pe = Expression.Parameter(typeof(Dictionary<string, object>), "x");
    var dictPropery = Expression.PropertyOrField(pe, "name"); // Dictonary value with respect to the name key
    var methodCall = Expression.Call(dictPropery, typeof(string).GetMethod("Contains"), Expression.Constant(filterParams["name"], typeof(string)));
    var lambda = Expression.Lambda<Func<Dictionary<string, object>, bool>>(methodCall, pe);
    return lambda;
}

What I'm trying to accomplish is the where clause of the query to get all the data for a report.

Expresssiom, that should be created as result of the method should look like:

x => ((string)x["name"]).Contains(term) 
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Ok, you need to change your method this way:

private Expression<Func<Dictionary<string, object>, bool>> 
                     GenerateWhereExpression(Dictionary<string, object> filterParams)
{
    var pe = Expression.Parameter(typeof(Dictionary<string, object>), "x");
    // it is call of x.getItem("name") what is the same as x["name"]
    var dictPropery = Expression.Call(pe, 
           typeof(Dictionary<string, object>).GetMethod("get_Item"), 
           Expression.Constant("name")); 

    //cast to ((string)x.getItem("name"))
    var castProperty = Expression.Convert(dictPropery, typeof(string));
    var methodCall = Expression.Call(castProperty, 
                    typeof(string).GetMethod("Contains"), 
                    Expression.Constant(filterParams["name"], typeof(string)));
    var lambda = Expression.Lambda<Func<Dictionary<string, object>, bool>>(methodCall, pe);
    return lambda;
}

I have tested it with my mongodriver and it seems to work. Actually, for getting Items from dictionary you need to call Item property (Building Expression Tree Using a Parameter's Indexer). But either i have used it wrong way, or MongoDb driver couldn't translate it properly, so i am doing it calling get_Item method, what is the same thing.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!