I am trying to query the mongodb database using AsQueryable and LINQ functions.
My current database structure is that I have some collections, mainly a record which are defined in C# like this:
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);
}
}
}
As you, data are mainly stored in Contents which is an ExpandoObject and that's where my issue is.
I am trying to do something like this:
var collection =
database.GetCollection<Record>("recordData").AsQueryable()
.Where(x => x.Contents.SingleOrDefault(z => z.Key == "dateOfBirth").Value.ToString().Length > 0)
.ToList();
but I get this exception:
System.InvalidOperationException HResult=0x80131509 Message={document}{contents}.SingleOrDefault(z => (z.Key == "dateOfBirth")).Value.ToString().Length is not supported.
Also, when I tried:
var collection1 = database.GetCollection<Record>("recordData").AsQueryable()
.Where(x => (DateTime)(x.Contents.SingleOrDefault(z => z.Key == "dateOfBirth").Value) > DateTime.Now)
.ToList();
I get this exception:
System.InvalidOperationException
HResult=0x80131509
Message=Convert({document}{contents}.SingleOrDefault(z => (z.Key == "dateOfBirth")).Value) is not supported.
Also, when I do this:
var collection =
database.GetCollection(“recordData”).AsQueryable()
.Where(x => (DateTime)x.Contents.First(z => z.Key == “dateOfBirth”).Value > DateTime.Now ).ToList();
I am getting this exception:
System.NotSupportedException: ‘The expression tree is not supported: {document}{contents}’
So my question really is that how can I run queries on an object which is of type ExpandoObject using AsQueryable and LINQ.
Thanks!