zoukankan      html  css  js  c++  java
  • EntityFramework 根据时间筛选数据

    需求:根据当前时间,获取条件合适的数据,其中截止时间只比较日期。

    1. 运行会报错的版本:

    var lifeWorkEventBatch = clientRepositoryContainer.LifeWorkEventBatchRepository.FindAll(lfe => lfe.SelectionStartTime <= DateTime.Now && lfe.SelectionEndTime >= DateTime.Now.Date).FirstOrDefault();

    这里查资料得知:sql里面没有根据当前日期再获取Date的函数,因此不能转换成功,直接报错。

    2.比较整个时间,不取出当前日期进行比较,有bug:

    var lifeWorkEventBatch = clientRepositoryContainer.LifeWorkEventBatchRepository.FindAll(lfe => lfe.SelectionStartTime <= DateTime.Now && lfe.SelectionEndTime >= DateTime.Now).FirstOrDefault();

    该linq转化为sql语句为:

        SELECT 
        [Extent1].[pkLifeWorkEventBatch] AS [pkLifeWorkEventBatch], 
        [Extent1].[LifeWorkEventBatchCode] AS [LifeWorkEventBatchCode], 
        [Extent1].[SelectionStartTime] AS [SelectionStartTime], 
        [Extent1].[SelectionEndTime] AS [SelectionEndTime], 
        [Extent1].[Status] AS [Status], 
        [Extent1].[Description] AS [Description], 
        [Extent1].[CreateOn] AS [CreateOn], 
        [Extent1].[CreateBy] AS [CreateBy], 
        [Extent1].[LastModifyOn] AS [LastModifyOn], 
        [Extent1].[LastModifyBy] AS [LastModifyBy]
        FROM [dbo].[LifeWorkEventBatch] AS [Extent1]
        WHERE ([Extent1].[SelectionStartTime] <= (SysDateTime())) AND ([Extent1].[SelectionEndTime] >= (SysDateTime()))

    3.实现需求的无错版本:

    var dateTimeNow = DateTime.Now;
                    var lifeWorkEventBatch = clientRepositoryContainer.LifeWorkEventBatchRepository.FindAll(lfe => lfe.SelectionStartTime <= dateTimeNow && lfe.SelectionEndTime >= dateTimeNow.Date).FirstOrDefault();

    转化后的sql

    exec sp_executesql N'SELECT 
        [Extent1].[pkLifeWorkEventBatch] AS [pkLifeWorkEventBatch], 
        [Extent1].[LifeWorkEventBatchCode] AS [LifeWorkEventBatchCode], 
        [Extent1].[SelectionStartTime] AS [SelectionStartTime], 
        [Extent1].[SelectionEndTime] AS [SelectionEndTime], 
        [Extent1].[Status] AS [Status], 
        [Extent1].[Description] AS [Description], 
        [Extent1].[CreateOn] AS [CreateOn], 
        [Extent1].[CreateBy] AS [CreateBy], 
        [Extent1].[LastModifyOn] AS [LastModifyOn], 
        [Extent1].[LastModifyBy] AS [LastModifyBy]
        FROM [dbo].[LifeWorkEventBatch] AS [Extent1]
        WHERE ([Extent1].[SelectionStartTime] <= @p__linq__0) AND ([Extent1].[SelectionEndTime] >= @p__linq__1)',N'@p__linq__0 datetime2(7),@p__linq__1 datetime2(7)',@p__linq__0='2019-07-31 12:52:40.5630849',@p__linq__1='2019-07-31 00:00:00'

    从两次转化后的sql语句可以看出,先获取系统日期,再在linq中使用,则直接转化成为了日期类型的参数。

  • 相关阅读:
    could not detect mdm peripheral on hardware
    学习zynq的一些感受
    sdk添加新的C文件编译出错
    linux下驱动webcam
    转:fatal error: SDL/SDL.h: No such file or directory
    转:Unknown module(s) in QT: multimedia
    HFSS设计导入AD中
    REST(Representational state transfer)的四个级别以及HATEOAS介绍
    Servlet CDI Example Analysis
    Introduction and use of Cookie and Session(Cookie&Session的介绍和使用)
  • 原文地址:https://www.cnblogs.com/tylertang/p/11277150.html
Copyright © 2011-2022 走看看