前情提要:

在c# mvc EF模式下 普通的列表展示页面,采用form提交的,后台视同request对象接收,如果涉及到时间搜索的情况,经常会搞错,现在明确一下怎么做。

一:Request

    /// <summary>
/// 发票信息
/// </summary>
public class InvoiceInforRequest : Request
{
     /// <summary>
/// 创建开票时间
/// </summary>
public DateTime? AddDateStart { get; set; }
public DateTime? AddDateEnd { get; set; }
}
public class Request : ModelBase
{
public Request()
{
PageSize = ;
} public int Top
{
set
{
this.PageSize = value;
this.PageIndex = ;
}
} public int PageSize { get; set; }
public int PageIndex { get; set; }
}

  

public class ModelBase
{
public ModelBase()
{
CreateDate = DateTime.Now;
} public virtual int ID { get; set; }
public virtual DateTime CreateDate { get; set; }
public virtual int CreateStaffID { get; set; }
public virtual string CreateStaffName { get; set; }
}

这里使用的就是InvoiceInforRequest中的AddDateStart 和AddDateEnd

二:页面form提交

 <form class="form-inline">
<div class="form-group">
<label class="control-label">
起止日期:</label>
@Html.TextBox("AddDateStart", ViewData["AddDateStart"], new { @class = "form-control Wdate", @onclick = "WdatePicker()", @style = "width:100px;" })
<span style="margin: 0 5px">-</span>
@Html.TextBox("AddDateEnd", ViewData["AddDateEnd"], new { @class = "form-control Wdate", @onclick = "WdatePicker()", @style = "width:100px;" })
</div>
<button class="btn btn-info">
<span class="glyphicon glyphicon-search"></span>&nbsp;查询</button>
</form>

三:Controller方法

public ActionResult Index(InvoiceInforRequest request)
{
request.CreateDate = new DateTime();
ViewData.Add("AddDateStart", DateTime.Now.AddMonths(-).ToCnDataString());
ViewData.Add("AddDateEnd", DateTime.Now.ToCnDataString());
//获取发票列表
//Stopwatch sw = new Stopwatch();
//sw.Start();
var invoiceInforList = this.OMService.GetInvoiceInforList(request);
//sw.Stop();
//var sss = sw.ElapsedMilliseconds;
return View(invoiceInforList);
}

四:BILL

 /// <summary>
/// 发票信息列表
/// </summary>
public IEnumerable<InvoiceInfor> GetInvoiceInforList(InvoiceInforRequest request)
{
using (var dbContext = new OMDbContext())
{
IQueryable<InvoiceInfor> invoiceinforiqueryable = dbContext.InvoiceInfor.Include("TaxControl").Include("Invoice").Include("Invoice.Branch");
if (request.Status != (int)EnumInvoiceInforStatus.NotOpen)
{
if (request.AddDateStart != null)
{
invoiceinforiqueryable = invoiceinforiqueryable.Where(i => i.BillingDate >= request.AddDateStart);
}
if (request.AddDateEnd != null)
{
              //结束时间加了23小时和59分钟
              //linq的语法总是不支持addhours、convert 可能是我版本的问题,mvc 4.0
request.AddDateEnd = Convert.ToDateTime(request.AddDateEnd).AddHours().AddMinutes();
invoiceinforiqueryable = invoiceinforiqueryable.Where(i => i.BillingDate <= request.AddDateEnd);
}
}
return invoiceinforiqueryable.OrderByDescending(i => i.ID).ToPagedList(request.PageIndex, request.PageSize);
}
}

五:结论:

  因为这种模式下的提交,时间选择插件最后选择的时间是只到天:“yyyy-MM-dd” ,而不是“yyyy-MM-dd hh:mm:ss”,但是数据库(sql)中的时间格式是datetime

所以要在时间查询的时候,最后想查的是今天的,但是结束时间应该是今天的最后一分钟或者一秒钟。

  谢谢,看懂了就懂了,看不懂的,也希望能帮到你。

时间查询缺少部分数据 mvc 解决方案的更多相关文章

  1. mysql根据时间查询前一天数据

    MySql数据库如何根据时间查询前一天的数据?本文整理了几个解决方法,有需要的朋友参考下.   本节内容:用MySql怎么根据时间查询前一天的数据. 例1: 代码示例: select * from t ...

  2. Oracle 查询时间在当天的数据

    要实现这个功能需要用到trunc这个函数对时间的操作 select trunc(sysdate) from dual --2014-12-27 今天的日期为2014-12-27 select trun ...

  3. 在数据库中(Oracle),根据时间查询数据: to_date()和to_char()函数

    1. to_date() 函数 1.1 格式 to_date("要转换的字符串","转换的格式")   //两个参数的格式必须匹配,否则会报错.即按照第二个参数 ...

  4. SqlServer:SqlServer(xpath,字段约束,时间查询,数据文件查询及还原,压缩备份)

    1.xpath --1.文档 select @data --2.任意级别是否存在price节点 select @data.exist('//price') --3.获取所有book节点 select ...

  5. MySQL - 查询今天的数据(以及昨天、本月、上个月、今年...) 查询Datetime 时间的数据

    1,查询当天(今天)的数据 1 SELECT * FROM `order` WHERE TO_DAYS(order_time) = TO_DAYS(NOW()) 2,查询昨天的数据 1 SELECT  ...

  6. MySQL主从复制——主库已有数据的解决方案

    在上篇文章中我们介绍了基于Docker的Mysql主从搭建,一主多从的搭建过程就是重复了一主一从的从库配置过程,需要注意的是,要保证主从库my.cnf中server-id的唯一性.搭建完成后,可以在主 ...

  7. 最完整的数据倾斜解决方案(spark)

    一.了解数据倾斜 数据倾斜的原理: 在执行shuffle操作的时候,按照key,来进行values的数据的输出,拉取和聚合.同一个key的values,一定是分配到一个Reduce task进行处理. ...

  8. 匿名类型 使用泛型T linq返回dynamic类型的匿名实体 如何把匿名类型.GetType()返回的对象传进泛型里面 EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题

    [100分]紧急求助:LinQ下使用IQueryable<T>如何将返回类型<T>使用匿名类型 问题描述如下:我有一个方法如下:public IQueryable Dissen ...

  9. ELK:ElasticSearch中有数据,Kibana查询不到数据

    ElasticSearch中有数据,Kibana查询不到数据 多数原因就是Linux的时区问题, 在linux输入date查看当前时间是否根本地相对应,不对应那么你就来对了, 解决方案一. 这个选择的 ...

随机推荐

  1. CI框架下nginx重写规则

    因为: server { listen ; server_name 222.73.130.124; location / { index index.html index.htm index.php; ...

  2. js闭包避免内存泄漏 减少内存使用 避免对象无法回收注意事项

    闭包 如果闭包的作用域中保存着一个 HTML 元素,则该元素无法被销毁.(下面代码来自高程) 闭包是 JavaScript 开发的一个关键方面:匿名函数可以访问父级作用域的变量. function a ...

  3. 悟道—位IT高管20年的职场心经(读书笔记一)

    悟道--一位IT高管20年的职场心经 第一章  修炼! 修炼! 别跟我谈事业,先把工作做好. 别跟我说理想,先把饭碗端好: 志不可天天立.道必须日日修.没有实力,一切皆为妄谈. 修炼是硬道理. 1.1 ...

  4. Spring技术内幕:Spring AOP的实现原理(三)

    生成SingleTon代理对象在getSingleTonInstance方法中完毕,这种方法时ProxyFactoryBean生成AopProxy对象的入口.代理对象会封装对target目标对象的调用 ...

  5. python之圆周率

    #!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: pi.py #Author: ...

  6. linux学习笔记13--命令head和tail

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  7. C++ 4种强制类型转换

    C++的四种强制类型转换为:static_cast.const_cast.reinterpret_cast和dynamic_cast 类型转换的一般形式:cast-name(expression); ...

  8. Windows下 Zmq v4.0.4的使用 (C++)

    最近在研究Zero的使用. 我在网上找了一下,发觉大部分博客都写得乱七八糟,又不写明什么版本,什么系统下的,我都不知道怎么样才能弄出个编译过的程序来,只好自己摸索了. 我是windows下 使用vs2 ...

  9. [Tips] Resolve error: server certificate verification failed.

    # sympton: piaoger@piaoger-ubuntu:~/w/temp$ git clone https://mygit/solidmcp/solidmcp.gitCloning int ...

  10. PAT003 List Leaves

    题目: Given a tree, you are supposed to list all the leaves in the order of top down, and left to righ ...