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

119
Views
Parece que no puedo acceder a los valores de Dictonary<string, object> mientras construyo una expresión linq dinámica

Estoy tratando de generar una expresión de linq en el núcleo de asp.net para MongoDB usando MongoDriver y no puedo acceder al valor de tiempo de ejecución de un diccionario con el generador de expresiones. ¡Gracias por adelantado!

 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; }

Lo que estoy tratando de lograr es la cláusula where de la consulta para obtener todos los datos de un informe.

Expresssiom, que debe crearse como resultado del método, debería verse así:

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

0

Ok, necesitas cambiar tu método de esta manera:

 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; }

Lo he probado con mi mongodriver y parece funcionar. En realidad, para obtener elementos del diccionario, debe llamar a la propiedad Item ( Construir árbol de expresión usando un indexador de parámetros ). Pero o lo he usado de manera incorrecta, o el controlador MongoDb no pudo traducirlo correctamente, así que lo estoy haciendo llamando al método get_Item , que es lo mismo.

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!