1

Given a class with IDictionary member

public class A 
{
    ...

    [BsonElement("cli")]
    public Guid ClientId { get; set; }

    [BsonDictionaryOptions(DictionaryRepresentation.Document)]
    [BsonElement("props")]
    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 learn.microsoft.com via reflection?

Many Thanks!

Update

Sample mongo document is as follows

{
    "_id" : ObjectId("5bad9a7b73d552637cc97b58"),
    "cli" : NUUID("02c39a78-316a-4756-b88c-b8313f6ef252"),

    ...

    "props" : {
        "Phone" : NumberLong(442076),
        "Location" : "North Ryde",
        "Start" : ISODate("2018-09-01T03:19:16.000Z"),
    }
}
Yugo
  • 63
  • 4
  • To make your question a little more complete, can you show us an example of the `Properties` field in the BSON document? – ProgrammingLlama Oct 10 '18 at 06:47
  • Possible duplicate of [Indentifying a custom indexer using reflection in C#](https://stackoverflow.com/questions/1347936/indentifying-a-custom-indexer-using-reflection-in-c-sharp) – thehennyy Oct 10 '18 at 06:58
  • The indexer is indeed "Item". If I run `typeof(IDictionary).GetProperties().Where(p => p.GetIndexParameters().Count() > 0) .Single();` returns `System.Object Item [System.String]` – Yugo Oct 10 '18 at 23:39

0 Answers0