akanieski
akanieski

Reputation: 71

How can I query a mongodb document with Dictionary<Enum, int>?

Using mongodb-csharp driver (https://github.com/mongodb/mongo-csharp-driver)

I have the following classes & enums:

public enum PropertyType {
  Unknown,
  Age,
  Weight,
  Gender
}
public class Data {
  public Dictionary<PropertyType, Int32> Props {get;set;}
}

I can read and save data that looks like this.

{
  Props: {
    1: 28,
    2: 220,
    3: 0
  }
}

What I cannot do is query for "Data" with Props[PropertyType.Age] == 28.. see below code:

var data  = from d in collection where d.Props[PropertyType.Age] == 28 select d;

The error I get is:

System.InvalidOperationException: 'data.Props.get_Item(PropertyType.Age) is not supported.'

Help me obiwan kenobi your my only hope.

Upvotes: 1

Views: 950

Answers (1)

Naren
Naren

Reputation: 308

Did you try

var data  = from d in collection where d.Props.Any(x => x.Key.Equals(PropertyType.Age) && x.Value == 28) select d;

Upvotes: 1

Related Questions