저는 프로젝트에서 일하고 있는데 날짜별로 레코드를 필터링하는 데 문제가 있습니다. 나는 엔티티의 레코드를 검색하는 일반적인 방법이 있는데, 내 searchQuery는 '2014-03-15'와 같고 레코드 datetime 형식은 '2014-03-15 00 : 00 : 00.000'이고, 내 SQL 쿼리는 datetime SQL 레코드를 날짜 형식으로 만 다음 내 쿼리와 비교하십시오. 문제점은 레코드에서 genereated 날짜가 내 쿼리에 대해 eqauls가 아니므로 datepart 함수를 사용하여 문자열 결과를 연결하지만 결과는 같은 '2014- 3 15'및 방법이 좋을 것 sqlfunctions 날짜에 문자열을 캐스팅 존재하는 경우, 일치의 원인이되지는 여기에 내 코드입니다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);
genereated SQL은
입니다나는 날짜를 비교하는 문자열을 사용하지 마십시오 sqlFunctions
어때 이것에 대해 : http://stackoverflow.com/questions/10304373/how-convert-string-to-date-t-sql – ganders
명확히하기 위해 : 당신은 datetime을 db로 저장하고 (datetime으로 저장), 그리고 당신은 "날짜"부분만을 포함하는 문자열 (db가 아닌 매개 변수로 주어진)과 이것을 비교하고 싶습니까? –
그래,하지만 주요 문제는 내가 sql 함수에 의해 날짜 형식으로 날짜 문자열을 변환해야하고 어떤 일을 할 수있는 함수가 없다, 내가 쿼리를 빌드하는 반사를 사용하고 있으며,이 문제를 해결하기가 더 힘들어 – user3333426