在EF查询中添加DateTime和TimeSpan的代码示例 [英] Code Example for Add DateTime and TimeSpan in EF query

查看:293
本文介绍了在EF查询中添加DateTime和TimeSpan的代码示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找这个问题的代码示例。

I am looking for a code example of this question.

使用LINQ的DateTime.Add(TimeSpan)

我需要将一个完整的TimeSpan添加到DateTime。

I need to add an entire TimeSpan to a DateTime.

我已经尝试了 SqlFunctions.DateAdd(ss,SqlFunctions.DatePart(s,b.duration)看起来只是添加了TimeSpan的秒数。

I already have tried SqlFunctions.DateAdd("ss", SqlFunctions.DatePart("s", b.duration) but this looks like it is only adding the the seconds part of the TimeSpan.

这是迄今为止的代码

var queryClash = from b in db.calEvents
                where (newEvent.startTime <= (SqlFunctions.DateAdd("ss", SqlFunctions.DatePart("ss", b.duration), b.startTime)))
                && (newEventEndTime >= b.startTime)
                select b;


推荐答案

如我所使用的评论中所建议的:

As suggested in the comments I used:

DbFunctions.AddMilliseconds(b.startTime, DbFunctions.DiffMilliseconds(b.duration, TimeSpan.Zero)) >= endtime)

这篇关于在EF查询中添加DateTime和TimeSpan的代码示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆