I'm working on a project and i have problem with filtering records by date. I have a generic method to search the records of any entities,my searchQuery is like '2014-03-15' and the records datetime format is '2014-03-15 00:00:00.000', My sql query should convert the datetime sql records to date format only and then compare it with my query, The problem is this that the genereated date from records are not eqauls to my query, I'm using the datepart function then concating the string result, but the result will be like ' 2014- 3- 15' and cause no matching, if a way exist to cast the string to date in sqlfunctions that would be great, Here is my code
var date = Expression.Convert(propExp, typeof(Nullable<DateTime>));
var datePart = typeof(SqlFunctions).GetMethod("DatePart",
new Type[] { typeof(string), typeof(Nullable<DateTime>) });
var month = Expression.Call(datePart, Expression.Constant("MM"), date);
var toDouble = Expression.Convert(month, typeof(Nullable<double>));
var monthPartToString = typeof(SqlFunctions).GetMethod("StringConvert",
new Type[] { typeof(Nullable<double>) });
var monthPart = Expression.Call(monthPartToString, toDouble);
var dd = Expression.Call(datePart, Expression.Constant("dd"), date);
var toDoubledd = Expression.Convert(dd, typeof(Nullable<double>));
var ddPartToString = typeof(SqlFunctions).GetMethod("StringConvert",
new Type[] { typeof(Nullable<double>) });
var ddPart = Expression.Call(ddPartToString, toDoubledd);
var yy = Expression.Call(datePart, Expression.Constant("yyyy"), date);
var toDoubleyy = Expression.Convert(yy, typeof(Nullable<double>));
var yyPartToString = typeof(SqlFunctions).GetMethod("StringConvert",
new Type[] { typeof(Nullable<double>) });
var yyPart = Expression.Call(yyPartToString, toDoubleyy);
var conCat4 = typeof(string).GetMethod("Concat",
new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) });
var conCat2 = typeof(string).GetMethod("Concat",
new Type[] { typeof(string), typeof(string) });
var delim = Expression.Constant("-");
stringProp = Expression.Call(conCat2, Expression.Call(conCat4, yyPart, delim, monthPart, delim), ddPart);
the genereated sql is
select * from orders where STR( CAST( DATEPART(yyyy, CreatedOn) AS float)) + N'-' + STR( CAST( DATEPART(MM,CreatedOn) AS float)) + N'-' + STR( CAST( DATEPART(dd, CreatedOn ) AS float)) LIKE N'%2014-03-15%'
I need a way to cast this string to date by sqlFunctions