光滑:筛选所有具有joda DateTime日期等于今天的记录 [英] Slick: Filtering all records which have a joda DateTime date equal to today

查看:89
本文介绍了光滑:筛选所有具有joda DateTime日期等于今天的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一种实现方法是这样的:

One way to achieve it would be like this:

val now = DateTime.now

val today = now.toLocalDate
val tomorrow = today.plusDays(1)

val startOfToday = today.toDateTimeAtStartOfDay(now.getZone)
val startOfTomorrow = tomorrow.toDateTimeAtStartOfDay(now.getZone)

val todayLogItems = logItems.filter(logItem =>
  logItem.MyDateTime >= startOfToday && logItem.MyDateTime < startOfTomorrow
).list

有什么方法可以更简洁地编写查询?符合以下条件的东西:

Is there any way to write the query in a more concise way? Something on the lines of:

logItems.filter(_.MyDateTime.toDate == DateTime.now.toDate).list

我之所以这样问,是因为在LINQ to NHibernate中是可以实现的(使用nhibernate仅通过日期部分比较来按日期获取记录).

I'm asking this because in LINQ to NHibernate that is achievable (Fetching records by date with only day part comparison using nhibernate).

推荐答案

除非Slick joda映射器添加了对比较的支持,否则除非您自己添加比较,否则您将不走运.为了给它一个机会,这些可能是有用的指针: * http://slick.typesafe.com/doc/2.0.0/userdefined. html * http://slick.typesafe.com/doc /2.0.0/api/#scala.slick.lifted.ExtensionMethods * https://github.com/slick/slick/blob/2.0.0/src/main/scala/scala/slick/lifted/ExtensionMethods.scala

Unless the Slick joda mapper adds support for comparisons you are out of luck unless you add it yourself. For giving it a shot these may be helpful pointers: * http://slick.typesafe.com/doc/2.0.0/userdefined.html * http://slick.typesafe.com/doc/2.0.0/api/#scala.slick.lifted.ExtensionMethods * https://github.com/slick/slick/blob/2.0.0/src/main/scala/scala/slick/lifted/ExtensionMethods.scala

我创建了一个票证,以便在某些时候在Slick中对其进行研究: https://github.com. com/slick/slick/issues/627

I create a ticket to look into it in Slick at some point: https://github.com/slick/slick/issues/627

这篇关于光滑:筛选所有具有joda DateTime日期等于今天的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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