Search code examples
c#mongodblinqmongodb-.net-driverexpression-trees

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) 

Solution

  • 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.