当前位置:网站首页>关于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组件——底部弹起的滚动选择器
- Summary of Aix storage management
- UDS bootloader of s32kxxx bootloader
- 测试人进阶技能:单元测试报告应用指南
- LeetCode 0241. Design priority for arithmetic expressions - DFS
- Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
- How does schedulerx help users solve the problem of distributed task scheduling?
- New version of free mobile phone, PC, tablet, notebook four terminal Website thumbnail display diagram online one click to generate website source code
- Output results of convolution operation with multiple tensors and multiple convolution kernels
- King combat power query renamed toolbox applet source code - with traffic main incentive advertisement
猜你喜欢

Leetcode skimming: binary tree 02 (middle order traversal of binary tree)

Viewing and modifying volume group attributes of Aix storage management (II)

XMind思维导图

RFID makes the inventory of fixed assets faster and more accurate

SSO single sign on implementation.

How do Lenovo computers connect Bluetooth headsets?

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

Zak's latest "neural information transmission", with slides and videos

使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)

Bc35 & bc95 onenet mqtt (old)
随机推荐
【CTF】bjdctf_2020_babystack2
DTL dephossite | prediction method of dephosphorylation sites based on Transfer Learning
cookie、session、tooken
To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"
Promise和模块块化编程
SSO single sign on implementation.
Recently, three articles in the nature sub Journal of protein and its omics knowledge map have solved the core problems of biology
【js通过url下载文件】
Global and Chinese markets of beverage seasoning systems 2022-2028: Research Report on technology, participants, trends, market size and share
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
Kuberntes cloud native combat high availability deployment architecture
2023款雷克萨斯ES产品公布,这回进步很有感
[opencv450] hog+svm and hog+cascade for pedestrian detection
sso单点登录的实现。
XMind思维导图
Use es to realize epidemic map or take out order function (including code and data)
工作中非常重要的测试策略,你大概没注意过吧
Evolution of Himalayan self-developed gateway architecture
2022 low voltage electrician examination questions and answers
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03