Asp.net mvc SqlFunctions将字符串转换为日期

Asp.net mvc SqlFunctions将字符串转换为日期,asp.net-mvc,linq,sql-function,datepart,sql-date-functions,Asp.net Mvc,Linq,Sql Function,Datepart,Sql Date Functions,我正在做一个项目,我在按日期筛选记录方面有问题。 我有一种搜索任何实体记录的通用方法,我的searchQuery类似于“2014-03-15”,记录的datetime格式为“2014-03-15 00:00:00.000”,我的sql查询应仅将datetime sql记录转换为日期格式,然后与我的查询进行比较,问题是,generateddatefrom记录不是查询的eqauls,我使用了datepart函数,然后将字符串结果合并,但结果如下 “2014-3-15”并导致不匹配,如果有一种方法可以

我正在做一个项目,我在按日期筛选记录方面有问题。 我有一种搜索任何实体记录的通用方法,我的searchQuery类似于“2014-03-15”,记录的datetime格式为“2014-03-15 00:00:00.000”,我的sql查询应仅将datetime sql记录转换为日期格式,然后与我的查询进行比较,问题是,generateddatefrom记录不是查询的eqauls,我使用了datepart函数,然后将字符串结果合并,但结果如下 “2014-3-15”并导致不匹配,如果有一种方法可以在sqlfunctions中强制转换字符串,这将非常好,下面是我的代码

                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);

我需要通过
sqlFunctions

不使用字符串来比较日期来将此字符串强制转换为日期永远。相反,请记住,从SQLServer2008开始,有一个
Date
类型只存储日期信息,没有时间组件。您应该生成将比较的两侧转换为该类型的sql,如下所示:

select * from orders where cast(CreatedOn As Date) = @MyDate

其中@MyDate已经是一个参数,您将包含在具有显式日期类型的查询中。永远不要生成在查询中直接替换值的查询。

这样如何:澄清一下:您的数据库中有一个datetime(存储为datetime),您想将其与只包含“日期”部分的字符串(作为参数给出,而不是在数据库中)进行比较?是,但主要的问题是,我应该通过sql函数将日期字符串转换为sql日期类型,而没有任何函数可以这样做,我使用反射来构建查询,这使得问题更难解决这里有两个问题1=>我不想使用日期来存储记录,因为我在其他查询中需要它2=>我知道如果我使用cast-as-date,问题将得到解决,但我使用的是sqlfunction,没有使用此函数的选项,我需要一种通过sqlFunctions将字符串转换为date的方法
select * from orders where cast(CreatedOn As Date) = @MyDate