LINQ2Entity 获取当天(0点到24点)的数据

本文探讨了在处理数据源时遇到的时间差问题,并提供了一种有效的方法来解决与当前时间相差一天的数据筛选问题。通过将当前日期转换为具体时间范围,实现了数据的有效筛选。
 看到标题,我想很多人都会马上想到用DateTime.Now.Date来进行对比,刚开始做的时候我也是这个样子的,但是去数据源的时候,系统报错,提示不支持Date属性,在网上看了很多解决办法,有一个可行的是:System.Data.Objects.EntityFunctions.DiffDays(a.FillTime , DateTime.Now) < -1  通过这个语句获取时间差,但是这个样子只能获取与当前时间相差一天的数据,并不能达到我想要的效果,后来想想,用下面办法解决了,。
  string str = DateTime.Now.ToShortDateString();

            DateTime time1 = Convert.ToDateTime(str + " 0:00:00");
            DateTime time2 = Convert.ToDateTime(str + " 23:59:59");

            var number = from a in context.Documents
                         where a.FillTime >= time1 & a.FillTime <= time2 
                         select a;

 记下来以防以后还会用!!

string StartTime = date.ToString("yyyy-MM-dd 00:00:00"); string EndTime = date.ToString("yyyy-MM-dd 23:59:59"); // 查询产量 var dbConifg = _oracleDbContext.API_SERVER_DB.Where(c => c.NEW_NAME == line).Select(a => new { a.ACC_LINE, a.CONN_STR }).FirstOrDefault(); if (dbConifg == null) { var dbConifgresponse = new { Code = 200, Msg = "No Mach Line conn date" }; return Ok(dbConifgresponse); } _connectionString = dbConifg.CONN_STR; var helper = new OracleHelper(_connectionString); StringBuilder CountQuery = new StringBuilder(); CountQuery.Append("SELECT "); CountQuery.Append("SUM(CASE WHEN \"RESULT\" = '1' THEN 1 ELSE 0 END) AS CountOK, "); CountQuery.Append("SUM(CASE WHEN \"RESULT\" = '0' THEN 1 ELSE 0 END) AS CountNOK "); CountQuery.Append("FROM acc_unithistory "); CountQuery.AppendFormat("WHERE line = '{0}' AND op = '{1}' ", dbConifg.ACC_LINE, op); CountQuery.AppendFormat("AND enddt BETWEEN TO_DATE('{0}', 'yyyy-MM-dd hh24:mi:ss') ", StartTime); CountQuery.AppendFormat("AND TO_DATE('{0}', 'yyyy-MM-dd hh24:mi:ss')", EndTime); var productionCountResult = await helper.ExecuteQueryAsync(CountQuery.ToString()); // 计算产量 int productionCount = 0; int productionCountOK = 0; int productionCountNOK = 0; if (productionCountResult != null && productionCountResult.Count > 0) { var row = productionCountResult[0]; productionCountOK = row.ContainsKey("CountOK") ? Convert.ToInt32(row["CountOK"]) : 0; productionCountNOK = row.ContainsKey("CountNOK") ? Convert.ToInt32(row["CountNOK"]) : 0; logger.Debug($"Production Count OK: {productionCountOK}, Production Count NOK: {productionCountNOK}"); } productionCount = productionCountOK + productionCountNOK; 查询生产量 需要参考 这个办法 ,因为 每个生产线的数据 都不在一起
最新发布
07-17
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值