entity vs. dapper datetime check not worker

asp.net-mvc dapper entity-framework

Question

I have entity and also use dapper, I have 1 form with 2 date fields... named before and after so users can search in between those dates. The one from entity works perfectly but the one from dapper does not work for some reason what could possibly be wrong here is the entity one

var article = (from x in db.Articles
                           where  x.created >= before && x.created <= after
                           select x);

and here is the one from dapper

var article = sqlConnection.Query<Article>("Select * from articles where created>=@befor AND created<=@afte ", new { befor = before, afte = after});

and yes I have all the connections for Dapper working as it does go to the database but for some reason its not picking records between those 2 dates..any suggestions..

Accepted Answer

dapper is just a wrapper around raw TSQL (with a slight caveat around in, where dapper adds some magic to make varadic "in" queries simpler). So; if it works in TSQL it should work fine in dapper, as long as your inputs make sense. For example, I am assuming that before and after in this example are typed as non-nullable DateTime, i.e.

DateTime before = ..., after = ...;
var article = sqlConnection.Query<Article>(
    "Select * from articles where created>=@befor AND created<=@afte ",
    new { befor = before, afte = after});

as a side note, it would perhaps be more obvious to just use:

DateTime before = ..., after = ...;
var article = sqlConnection.Query<Article>(
    "Select * from articles where created>=@before AND created<=@after",
    new { before, after });

but fundamentally, as long as those parameters have non-null values that are DateTimes, it should work fine.


Popular Answer

The problem here could be that the before and after values are send as string and the current locale may be causing the date to be interpreted incorrectly.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why