Linq-To-Entities中是否有DateTime.Ticks或DateTime.TimeOfDay的替代方案?

3
我正在为一个asp.net mvc2 web应用程序编写日志解析器。我使用Entity框架作为模型,同时使用我的手动引擎和SqlServer2008 CDC功能进行日志记录。
当向数据库插入或编辑行时,会记录下操作。但是,在实际表中发生更改和记录这些更改之间存在一小段时间差。当用户单击其中某些内容时,我需要显示CDC表中的详细信息。由于前面提到的时间差,我无法比较两个DateTime值的等价性。我想允许2000毫秒的延迟。我所知道的最简单的方法是使用Ticks或TimeOfDay,并比较它们的值相减的绝对值,但该死的Linq-To-Entities不允许使用这两个属性。
以下是我遇到问题的简单函数...
        public static List<dbo_Object_CT> GetLogDetails (Int32 objectID, DateTime? actionDateAndTime)
    {
        ObjectEntities oe = new ObjectEntities();

        var mylogdetails = (from objectLog in oe.dbo_Object_CT
                     join ltm in oe.lsn_time_mapping on objectLog.C___start_lsn equals ltm.start_lsn
                     where (objectLog.Id == objectID)
                     && ((actionDateAndTime == null) ? true : (Math.Abs(actionDateAndTime.Value.Ticks - ltm.tran_begin_time.Value.Ticks) < 2000))
                     select objectLog).ToList();

        return mylogdetails;
    }

我知道我可以手动编写“where”子句,但那会很冗长、难看且慢。有没有更好的建议?

2个回答

4

0

关于什么?

((TimeSpan) (actionDateAndTime.Value - ltm.tran_begin_time.Value)).Milliseconds > 2000 

生成类似于SQL的内容

(CONVERT(Int,(CONVERT(BigInt,(CONVERT(BigInt,(((
 CONVERT(BigInt,DATEDIFF(DAY, t1, t2))) 
 * 86400000) 
 + DATEDIFF(MILLISECOND, DATEADD(DAY, DATEDIFF(DAY, t1, t2), t1), t2)) 
 * 10000)) / 10000)) % 1000)) 

不知道如何让LINQ生成类似SQL的语句

 DATEDIFF (MS , actionDateAndTime , ltm.tran_begin_time)  

我正在尝试类似的东西,不过必须使用"TotalMiliseconds",因为"Miliseconds"只返回DateTime的毫秒部分(当你从DateTime中减去DateTime时,也会得到DateTime)。目前我还没有成功,如果成功了,我会报告的(当然会投赞成票;))。 - Eedoh
哎呀,忘记了TotalMilliseconds和Milliseconds之间的区别。 还忘了需要使用Math.Abs(..)。 - sgmoore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接