MongoDB C# Why can't you use DateTime.Date with IQueryable? -
i have method set in mongodb dal class.
public iqueryable<mymodel> retrieve(expression<func<mymodel, bool>> expression) { if (!bsonclassmap.isclassmapregistered(typeof(mymodel))) { domapping(); } var client = new mongoclient(mongoconnectionstring); var database = client.getdatabase("databasename"); var documents = database.getcollection<mymodel>("mymodeltable"); return documents.asqueryable<mymodel>().where(expression); }
i want simple
var result = retrieve(a => a.somedateproperty.date >= startdate && a.somedateproperty.date <= enddate);
however, every time try, error stating:
an exception of type 'system.invalidoperationexception' occurred in mongodb.driver.dll not handled in user code
additional information: {document}{somedateproperty}.date not supported.
i using official c# driver version 2.2.4.26.
is there way query on date? i've seen posts using dbfunctions.truncate, in entityframework libraries, stay away from.
i had same problem using date
, used:
idea use 2 borders (datetime.date
datetime
0 hours, 0 minutes... , second next day 0 hours, 0 minutes...).
datetime currentdaystart = datetime.now.date; datetime currentdayends = datetime.now.date.adddays(1); var result = retrieve(a => a.somedateproperty >= currentdaystart && a.somedateproperty < currentdayends);
and works me.