当前位置:网站首页>关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
2022-07-02 00:59:00 【罗马苏丹默罕默德】
1.DateTime是值类型(基础数据类型),其不可为Null,故不可以作为参数赋默认值
[HttpGet]
public StandResult<List<Role>> Test( DateTime start = DateTime.MinValue.,DateTime end = DateTime.MaxValue)
{
//DateTime作为方法的参数,不能设置默认值如上,这样是会报语法错误的
........
}
2.不要在方法里对DateTime参数进行判空操作,因为当你不为DateTime传入值时,它会被默认设置为 DateTime.MinValue
[HttpGet]
public StandResult<List<Role>> QueryRoles( DateTime start,DateTime end)
{
//不要对DateTime进行判空操作,它不会为Null,如果要用DateTime作为方法参数,请手动设置一个初始值
if(start!=null)
......
if(end != null)
......
}
测试:


故
在一些多条件的查询方法中,对于DateTime参数应该做一个小的特殊处理,以避免不传入的情况下使用了DateTime的默认值
[HttpGet]
public StandResult<List<Permission>> Query_Permissions([Required]int PageIndex,[Required]int PageSize,
string name, string type, string createEmp,DateTime start,DateTime end,long Id = -1)
{
//像这个方法,我用两个DateTime参数来限制查询时间,
//按上述的情况,不过不传入start和end日期,它们它们都是{0000-00-00....}的最小时间
//带入查询条件,则 time>DateTime.Minvalue&&time<DateTime.Minvalue是一个时间悖论,导致条件查询直接失效
if (start == DateTime.MinValue)
start = Convert.ToDateTime("1970-01-01");
if (end == DateTime.MinValue)
end = Convert.ToDateTime("9999-12-12");
List<Permission> permissions = scoper.GetTool().Queryable<Permission>()
.WhereIF(!string.IsNullOrEmpty(name), it => it.Name == name)
.WhereIF(!string.IsNullOrEmpty(type), it => it.Type == type)
.WhereIF(!string.IsNullOrEmpty(createEmp), it => it.Create_Emp == createEmp)
.WhereIF(start!=null,it=>it.Create_Time>start) //如果不加上面的初始化,当参数不传入日期类型
.WhereIF(end != null, it => it.Create_Time < end) //会直接使用两个相同的最小日期判断,造成BUG
.WhereIF(Id != -1, it => it.Id == Id)
.ToPageList(PageIndex, PageSize, ref Total);
}
最后一个建议,直接让日期参数必须填【Required】写可以完美解决这个问题,(必须判断结束日期大于开始日期)
边栏推荐
- 【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器
- What does open loop and closed loop mean?
- @Valid parameter verification does not take effect
- 【js通过url下载文件】
- Excel PivotTable
- DTL dephossite | prediction method of dephosphorylation sites based on Transfer Learning
- The 8-year salary change of testers makes netizens envy it: you pay me one year's salary per month
- Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
- Common loss function of deep learning
- Cookie, session, tooken
猜你喜欢

Minimize the error

Export default the exported object cannot be deconstructed, and module Differences between exports
![[eight sorts ②] select sort (select sort, heap sort)](/img/4b/da0d08230391d6ee48cd8cfd2f7240.png)
[eight sorts ②] select sort (select sort, heap sort)

PLC Analog input analog conversion FB s_ ITR (Mitsubishi FX3U)

RFID让固定资产盘点更快更准

Slf4j print abnormal stack information

一名优秀的软件测试人员,需要掌握哪些技能?

Tensorflow tensor convolution, input and convolution kernel dimension understanding

SSO single sign on implementation.

测试员8年工资变动,令网友羡慕不已:你一个月顶我一年工资
随机推荐
What skills does an excellent software tester need to master?
Advanced skills of testers: a guide to the application of unit test reports
How to determine whether the current script is in the node environment or the browser environment?
XMIND mind map
Powerful calendar wechat applet source code - support the main mode of doing more traffic
Mitsubishi PLC FX3U pulse axis jog function block (mc_jog)
SSO single sign on implementation.
New version of free mobile phone, PC, tablet, notebook four terminal Website thumbnail display diagram online one click to generate website source code
excel查找与引用函数
449-原码、补码、反码
JS common library CDN recommendation
AIX存储管理之逻辑卷的创建及属性的查看和修改
Creation of volume group for AIX storage management (I)
How to type spaces in latex
2022 safety officer-a certificate examination questions and online simulation examination
Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
Weather forecast applet source code weather wechat applet source code
2022 safety officer-b certificate examination practice questions simulated examination platform operation
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
Summary of Aix storage management