当前位置:网站首页>关于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】写可以完美解决这个问题,(必须判断结束日期大于开始日期)
边栏推荐
- Global and Chinese markets for context and location-based services 2022-2028: Research Report on technology, participants, trends, market size and share
- 2022拼多多详情/拼多多商品详情/拼多多sku详情
- 教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
- PLC Analog input analog conversion FB s_ ITR (Mitsubishi FX3U)
- [eight sorts ①] insert sort (direct insert sort, Hill sort)
- Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
- The 8-year salary change of testers makes netizens envy it: you pay me one year's salary per month
- Synthetic watermelon game wechat applet source code / wechat game applet source code
- 2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
- Deb file installation
猜你喜欢
2022 low voltage electrician examination questions and answers
2022拼多多详情/拼多多商品详情/拼多多sku详情
PLC Analog input analog conversion FB s_ ITR (Mitsubishi FX3U)
JS -- image to base code, base to file object
【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
With the acquisition of Xilinx, AMD is more than "walking on two legs" | Jiazi found
Leetcode skimming: stack and queue 02 (realizing stack with queue)
工作中非常重要的测试策略,你大概没注意过吧
Kuberntes cloud native combat high availability deployment architecture
2023款雷克萨斯ES产品公布,这回进步很有感
随机推荐
"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
Global and Chinese markets for context and location-based services 2022-2028: Research Report on technology, participants, trends, market size and share
gradle
UDS bootloader of s32kxxx bootloader
How to determine whether the current script is in the node environment or the browser environment?
[conference resources] the Third International Conference on Automation Science and Engineering in 2022 (jcase 2022)
Advanced skills of testers: a guide to the application of unit test reports
Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
[opencv450] hog+svm and hog+cascade for pedestrian detection
Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
Datawhale 社区黑板报(第1期)
Global and Chinese markets for food allergens and intolerance tests 2022-2028: Research Report on technology, participants, trends, market size and share
测试人进阶技能:单元测试报告应用指南
Comprehensive broadcast of global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
[eight sorts ①] insert sort (direct insert sort, Hill sort)
教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master
cookie、session、tooken
JS -- image to base code, base to file object
Global and Chinese markets for distributed generation and energy storage in telecommunications networks 2022-2028: Research Report on technology, participants, trends, market size and share