Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

121
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda