我有以下 LINQ-to-Entities 查询,用于检索与给定日期()的日期部分匹配的记录dte
:
Dim qry = entities.Works.Where(Function(w) w.JobID = RecordID And
SqlFunctions.DatePart("y", w.FromDate) = SqlFunctions.DatePart("y", dte) And
SqlFunctions.DatePart("m", w.FromDate) = SqlFunctions.DatePart("m", dte) And
SqlFunctions.DatePart("d", w.FromDate) = SqlFunctions.DatePart("d", dte))
我想重用这个表达式,首先写这样的东西:
Public SqlDateEquals As System.Linq.Expressions
.Expression(Of Func(Of DateTime, DateTime, Boolean)) = Function(dte1, dte2)
SqlFunctions.DatePart("y", dte1) = SqlFunctions.DatePart("y", dte2) And
SqlFunctions.DatePart("m", dte1) = SqlFunctions.DatePart("m", dte2) And
SqlFunctions.DatePart("d", dte1) = SqlFunctions.DatePart("d", dte2)
然后,在其他查询中:
Dim qry = entities.Works.Where(Function(w) w.JobID =
RecordID And SqlDateEquals(w.FromDate, dte))
我的理解是 LINQ-to-Entities 解析传入的表达式.Where
,它也应该能够识别SqlFunctions.DatePart
为表达式的一部分。因此,包含的表达式SqlFunctions.DatePart
应该仍然可以解析为它们的等效 SQL。
我怎样才能做到这一点?