mardi 9 octobre 2018

Expression Access IDictionary specific element in the collection without Item

Given a class with IDictionary member

public class A 
{
    ...
    public IDictionary<string, object> Properties { get; set; }
    ...
}

I'm trying to build an expression tree for GroupBy for MongoDB C# driver specifically. For example:

var test = baseQuery.GroupBy(x => new Tuple<Guid, object>(x.ClientId, x.Properties["Location"]));

My problem is on emitting correct Expression for x.Properties["Location"] that is acceptable by Mongo.

If I attempt to use PropertyIndexer as explained in another stackoverflow question which basically emits {x.Properties.Item["Location"], Mongo will complain:

System.NotSupportedException: $project or $group does not support {document}{props}.Item["Location"].

I suspect the offending issue is the extra "Item" on the expression, as I can successfully execute the query above without reflection.

How can I access the Dictionary as this[TKey] as mentioned in the docs.microsoft.com via reflection?

Many Thanks!





Aucun commentaire:

Enregistrer un commentaire