title: Filtering using AsQueryable in Mongodb C# - ExpandoObject
body: <p>I am trying to query the mongodb database using AsQueryable and LINQ functions.</p>
  
  <p>My current database structure is that I have some collections, mainly a record which are defined in C# like this:</p>
  
  <pre><code>public class Record  
      {
          [BsonElement("formName")]
          public string FormName { get; set; }
  
          [BsonElement("created")]
          public DateTime Created { get; set; }
  
          [BsonElement("createdBy")]
          public string CreatedBy { get; set; }
  
          [BsonId]
          [BsonIgnoreIfDefault]
          [BsonRepresentation(BsonType.ObjectId)]
          private string InternalId { get; set; }
  
          [BsonElement("recordId")]
          [Newtonsoft.Json.JsonProperty("recordId")]
          public string Id { get; set; }
  
          [BsonElement("organisationId")]
          public string OrganisationId { get; set; }
  
          // TODO: Consider making configurable
          private string appName = "MediLog";
  
          [BsonElement("appName")]
          public string AppName
          {
              get { return this.appName; }
              set { this.appName = value; }
          }
  
          [BsonElement("schemaVersion")]
          public int SchemaVersion { get; set; }
  
          [BsonElement("contents")]
          public ExpandoObject Contents { get; set; }
  
          [BsonElement("modified")]
          public DateTime Modified { get; set; }
  
          [BsonElement("modifiedBy")]
          public string ModifiedBy { get; set; }
  
          [BsonElement("majorVersion")]
          public int MajorVersion { get; private set; }
  
          [BsonElement("minorVersion")]
          public int MinorVersion { get; private set; }
  
          [BsonElement("version")]
          public string Version
          {
              get
              {
                  return (MajorVersion + "." + MinorVersion);
              }
  
              set
              {
                  MajorVersion = Convert.ToInt32(value?.Substring(0, value.IndexOf(".", StringComparison.OrdinalIgnoreCase)), CultureInfo.InvariantCulture);
                  MinorVersion = Convert.ToInt32(value?.Substring(value.IndexOf(".", StringComparison.OrdinalIgnoreCase) + 1), CultureInfo.InvariantCulture);
              }
          }
  
      }
  </code></pre>
  
  <p>As you, data are mainly stored in <code>Contents</code> which is an <code>ExpandoObject</code> and that's where my issue is. </p>
  
  <p>I am trying to do something like this:</p>
  
  <pre><code> var collection =
                  database.GetCollection&lt;Record&gt;("recordData").AsQueryable()
                      .Where(x =&gt;  x.Contents.SingleOrDefault(z =&gt; z.Key == "dateOfBirth").Value.ToString().Length &gt; 0)
                          .ToList();  
  </code></pre>
  
  <p>but I get this exception:</p>
  
  <p>System.InvalidOperationException
    HResult=0x80131509
    Message={document}{contents}.SingleOrDefault(z => (z.Key == "dateOfBirth")).Value.ToString().Length is not supported.</p>
  
  <p>Also, when I tried:</p>
  
  <pre><code>  var collection1 = database.GetCollection&lt;Record&gt;("recordData").AsQueryable()
                  .Where(x =&gt; (DateTime)(x.Contents.SingleOrDefault(z =&gt; z.Key == "dateOfBirth").Value)  &gt; DateTime.Now)
                  .ToList();
  </code></pre>
  
  <p>I get this exception:</p>
  
  <pre><code>System.InvalidOperationException
    HResult=0x80131509
    Message=Convert({document}{contents}.SingleOrDefault(z =&gt; (z.Key == "dateOfBirth")).Value) is not supported.
  </code></pre>
  
  <p>Also, when I do this:</p>
  
  <pre><code>var collection =
  database.GetCollection(“recordData”).AsQueryable()
  .Where(x =&gt; (DateTime)x.Contents.First(z =&gt; z.Key == “dateOfBirth”).Value &gt; DateTime.Now ).ToList();
  </code></pre>
  
  <p>I am getting this exception:</p>
  
  <pre><code>System.NotSupportedException: ‘The expression tree is not supported: {document}{contents}’
  </code></pre>
  
  <p>So my question really is that how can I run queries on an object which is of type ExpandoObject using AsQueryable and LINQ. </p>
  
  <p>Thanks!</p>