Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda