实体SQL比较datetime而不是毫秒

时间:2021-04-09 09:33:41

I'm trying to fetch some records from MSSQL DB using EntityObject with EntitySQL query. The field i'm using to filter is type of datetime. The generated query projected without millisecond, to the SQL Server returns nothing. When i'm adding the milliseconds, i get results.

我正在尝试使用带有EntitySQL查询的EntityObject从MSSQL DB中获取一些记录。我用来过滤的字段是日期时间的类型。生成的查询在没有毫秒的情况下投射到SQL Server,不返回任何内容。当我添加毫秒时,我得到结果。

How can i use the EntitySQL to get result without the milliseconds?

如何在没有毫秒的情况下使用EntitySQL获取结果?

thanks.

3 个解决方案

#1


It's not elegant, but this worked for me:

它不优雅,但这对我有用:

foos.SingleOrDefault(f => f.EntryDate.Year == bar.EntryDate.Year &&
                          f.EntryDate.Month == bar.EntryDate.Month &&
                          f.EntryDate.Day == bar.EntryDate.Day &&
                          f.EntryDate.Hour == bar.EntryDate.Hour &&
                          f.EntryDate.Minute == bar.EntryDate.Minute &&
                          f.EntryDate.Second == bar.EntryDate.Second);

#2


One way to do it is to create a view of your data, where your datetime column is converted to smalldatetime (which does not have milliseconds).

一种方法是创建数据视图,其中datetime列转换为smalldatetime(没有毫秒)。

Then add the view to your entity framework model, and read the data through the view.

然后将视图添加到实体框架模型,并通过视图读取数据。

Hope this helps

希望这可以帮助

Shiraz

#3


Workaround I found is on initial fetch store date as .ToBinary() then when you filter just do new DateTime(binaryValue) and compare with that.

我找到的解决方法是在初始获取存储日期为.ToBinary()然后当您过滤时只执行新的DateTime(binaryValue)并与之进行比较。

#1


It's not elegant, but this worked for me:

它不优雅,但这对我有用:

foos.SingleOrDefault(f => f.EntryDate.Year == bar.EntryDate.Year &&
                          f.EntryDate.Month == bar.EntryDate.Month &&
                          f.EntryDate.Day == bar.EntryDate.Day &&
                          f.EntryDate.Hour == bar.EntryDate.Hour &&
                          f.EntryDate.Minute == bar.EntryDate.Minute &&
                          f.EntryDate.Second == bar.EntryDate.Second);

#2


One way to do it is to create a view of your data, where your datetime column is converted to smalldatetime (which does not have milliseconds).

一种方法是创建数据视图,其中datetime列转换为smalldatetime(没有毫秒)。

Then add the view to your entity framework model, and read the data through the view.

然后将视图添加到实体框架模型,并通过视图读取数据。

Hope this helps

希望这可以帮助

Shiraz

#3


Workaround I found is on initial fetch store date as .ToBinary() then when you filter just do new DateTime(binaryValue) and compare with that.

我找到的解决方法是在初始获取存储日期为.ToBinary()然后当您过滤时只执行新的DateTime(binaryValue)并与之进行比较。