前情提要:

在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. 一些JS常用的方法

    /** * JS公用类库文件 */ (function(){ Tools = { W: window, D: document, Postfix: ".php", GetId: f ...

  2. LaTeX数学公式输入

    [置顶 Tips ] 在 WinEdt 中快速添加公式字符而不必手动打出一个个letters~: 即会出现如下 GUI Page Control : ------------------------- ...

  3. Hacker - 世界上第一个黑客

    http://juliet.iteye.com/blog/176525凯文·米特尼克,1964年生于美国加州的洛杉矶. 13岁时他对电脑着了迷,掌握了丰富的计算机知识和高超的操作技能,但却因为用学校的 ...

  4. jmeter ---测试TCP服务器/模拟发送TCP请求

    jmeter测试TCP服务器/模拟发送TCP请求 jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响 ...

  5. Rabbitmq消息队列(六) 主题交换机

    1.简介 前面学习了有选择性的接收消息,但是却没有办法基于多个标准来接收消息.为了实现这个目的,接下来我们学习如何使用另一种更复杂的交换机 —— 主题交换机. 2.主题交换机 发送到主题交换机(top ...

  6. c++ 头文件循环引用解法

    A.h #include "B.h" class A{ public: B* m_b; } B.h #include "A.h" class B{ public ...

  7. [k8s]简单启动一个k8s集群

    简单启动一个k8s集群 kube-master mkdir -p /root/logs/api-audit /root/logs/controller /root/logs/scheduler kub ...

  8. twemproxy源码分析1——入口函数及启动过程

    最近工作中需要写一个一致性哈希的代理,在网上找到了twemproxy,结合网上资料先学习一下源码. 一.Twemproxy简介 Twemproxy是memcache与redis的代理,由twitter ...

  9. 735. Replace With Greatest From Right【medium】

    Given an array of integers, replace every element with the next greatest element (greatest element o ...

  10. ASP.NET Web API 使用Swagger生成在线帮助测试文档,支持多个GET

    以下为教程: 在现有webapi项目中,nuget安装以下两个插件 swagger.net.ui swashbuckle 安装完毕后可以卸载Swagger.NET,此处不需要! 安装完毕后屏蔽以下代码 ...