时间查询缺少部分数据 mvc 解决方案
前情提要:
在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> 查询</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 解决方案的更多相关文章
- mysql根据时间查询前一天数据
MySql数据库如何根据时间查询前一天的数据?本文整理了几个解决方法,有需要的朋友参考下. 本节内容:用MySql怎么根据时间查询前一天的数据. 例1: 代码示例: select * from t ...
- Oracle 查询时间在当天的数据
要实现这个功能需要用到trunc这个函数对时间的操作 select trunc(sysdate) from dual --2014-12-27 今天的日期为2014-12-27 select trun ...
- 在数据库中(Oracle),根据时间查询数据: to_date()和to_char()函数
1. to_date() 函数 1.1 格式 to_date("要转换的字符串","转换的格式") //两个参数的格式必须匹配,否则会报错.即按照第二个参数 ...
- SqlServer:SqlServer(xpath,字段约束,时间查询,数据文件查询及还原,压缩备份)
1.xpath --1.文档 select @data --2.任意级别是否存在price节点 select @data.exist('//price') --3.获取所有book节点 select ...
- MySQL - 查询今天的数据(以及昨天、本月、上个月、今年...) 查询Datetime 时间的数据
1,查询当天(今天)的数据 1 SELECT * FROM `order` WHERE TO_DAYS(order_time) = TO_DAYS(NOW()) 2,查询昨天的数据 1 SELECT ...
- MySQL主从复制——主库已有数据的解决方案
在上篇文章中我们介绍了基于Docker的Mysql主从搭建,一主多从的搭建过程就是重复了一主一从的从库配置过程,需要注意的是,要保证主从库my.cnf中server-id的唯一性.搭建完成后,可以在主 ...
- 最完整的数据倾斜解决方案(spark)
一.了解数据倾斜 数据倾斜的原理: 在执行shuffle操作的时候,按照key,来进行values的数据的输出,拉取和聚合.同一个key的values,一定是分配到一个Reduce task进行处理. ...
- 匿名类型 使用泛型T linq返回dynamic类型的匿名实体 如何把匿名类型.GetType()返回的对象传进泛型里面 EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题
[100分]紧急求助:LinQ下使用IQueryable<T>如何将返回类型<T>使用匿名类型 问题描述如下:我有一个方法如下:public IQueryable Dissen ...
- ELK:ElasticSearch中有数据,Kibana查询不到数据
ElasticSearch中有数据,Kibana查询不到数据 多数原因就是Linux的时区问题, 在linux输入date查看当前时间是否根本地相对应,不对应那么你就来对了, 解决方案一. 这个选择的 ...
随机推荐
- sql server 数据加密
select EncryptByPassPhrase('hksjlm',qlrmc)jmsj,* into jmqlr from qlr where qlbh in('5201411120005',' ...
- python内置函数、匿名函数、递归
python3--内置函数 内置函数: 截止到python 3.6.2 版本,现在python一共提供了68个内置函数:即python提供给你直接可以拿来使用的所有函数. 内置函数 (点击函数查 ...
- Python: 去掉字符串中的非数字(或非字母)字符
>>> crazystring = ‘dade142.;!0142f[.,]ad’ 只保留数字>>> filter(str.isdigit, crazystring ...
- android 签名、混淆打包
1.android 签名 使用eclipse导出带签名的apk,最简单的方式. 最后一步finish ,就能导出一个xxx.keystore的文件了. 下次再发布新版本的时候,使用这个生成的签名继续使 ...
- word2vector 理解入门
1.什么是word2vector? 我们先来看一个问题,假如有一个句子 " the dog bark at the mailman". 假如用向量来表示每个单词,我们最先想到的是用 ...
- instancetype 对比 id 的好处
转自:http://blog.csdn.net/yangzychina/article/details/8818941 今天研究代码的时候,发现居然返回 instancetype 类型,很惊讶自己学识 ...
- 413. Reverse Integer【easy】
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- iptables常用规则:屏蔽IP地址、禁用ping、协议设置、NAT与转发、负载平衡、自定义链
iptables常用规则:屏蔽IP地址.禁用ping.协议设置.NAT与转发.负载平衡.自定义链 时间 -- :: IT社区推荐资讯 原文 http://itindex.net/detail/4772 ...
- nyoj 742 子串和再续 类似 HDU 1024
子串和再续 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给你一个序列 S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000 ...
- PHP——smarty模板(第一天)
smarty.class.php 主要的东西放在类里面templates 放模板templates_c 放缓存 类里面 $smarty->assign("author",&q ...