本文介绍了检查日期是否重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我想检查日期是否相互重叠,否则给个帖子 我有这种方法:Hi everybody,I want to check if the dates are not overlap each other, otherwise give a messageI have this method:<pre lang="xml">public bool IsOverlapping(List <AgendaEventViewModel> viewModel, AgendaEventViewModel newItem) { bool isOverlapping = false; ReservationModel r = new ReservationModel(int.Parse(viewModel.id)); if (r.Begin == r.Begin) { } // TODO: Check if Event is Overlapping with other events :) Dynamic RoomRules Check should implemented here if (viewModel.Any(m => DateTime.Parse(m.start) < DateTime.Parse( newItem.start) && DateTime.Parse(newItem.start) < DateTime.Parse(m.end) ?? DateTime.MaxValue))); { this.ShowMessage(Message.Type.Info, Resources.AgendaEvent.IsOverlapping); } return isOverlapping; } 这是型号:and this is the model:public class AgendaEventViewModel { public string ReturnUrl { get; set; } public string id { get; set; } public int? funeralId { get; set; } public int? reservationId { get; set; } public int? roomId { get; set; } [Display(Name = "Title", ResourceType = typeof(Resources.AgendaEvent))] public string title { get; set; } [Display(Name = "Start", ResourceType = typeof(Resources.AgendaEvent))] public string start { get; set; } [Display(Name = "End", ResourceType = typeof(Resources.AgendaEvent))] public string end { get; set; } public string top { get; set; } public string left { get; set; } public List<RoomItemModel> RoomItems { get; set; } public List<CateringItemModel> CateringItems { get; set; } [Display(Name = "SeatingCapacity", ResourceType = typeof(Resources.AgendaEvent))] public int? SeatingCapacity { get; set; } //Max Room SeatingCapacity [Display(Name = "ExpectedSeatingCapacity", ResourceType = typeof(Resources.AgendaEvent))] public int? ExpectedSeatingCapacity { get; set; } // User Chosen } 但现在我收到错误消息: 运算符'??'不能应用于'bool'类型的操作数和'System.DateTime' 谢谢 哦,我现在有这样的: b $ bbut now I get the error message:Operator '??' cannot be applied to operands of type 'bool' and 'System.DateTime'thank youOke, I have it now like this:<pre lang="cs">public bool IsOverlapping(AgendaEventViewModel viewModel, AgendaEventViewModel newEvent) { bool isOverlapping = false; // ReservationModel r = new ReservationModel(int.Parse(viewModel.id)); DateTime? begin = DateTime.Parse(viewModel.start); DateTime? end = DateTime.Parse(viewModel.end); DateTime? begin2 = DateTime.Parse(newEvent.start); DateTime? end2 = DateTime.Parse(newEvent.end); //DateTime dataToCheck; // TODO: Check if Event is Overlapping with other events :) Dynamic RoomRules Check should implemented here if (begin == begin2) { this.ShowMessage(Message.Type.Info, Resources.AgendaEvent.IsOverlapping); isOverlapping = true; } return isOverlapping; } 但是如果我现在每次添加第二个calenderItem,即使是有一天它每次都会命中:But If I now add a second calenderItem then everytime, even on a other day it hits every time:<pre lang="cs">this.ShowMessage(Message.Type.Info, Resources.AgendaEvent.IsOverlapping); isOverlapping = true; 谢谢 似乎begin将与begin2相同。Thank youIt seems that begin will become the same as begin2.推荐答案 DateTime不是引用,也不可为空。你必须使用Nullable< datetime>或者那个结构看起来是C#。 ??仅当类型可以为null时才起作用。你应该重写条件,所以它不使用?并且你将全部完成。 你也错过了新事件在m.end之后结束但在m.start之后开始并且newEvent开始的地方在模型间隔内结束(如果您的模型中有可能) 如果这有帮助,请花些时间接受解决方案。谢谢。DateTime is not reference and is not nullable. You have to use Nullable<datetime> or however that structure looks in C#.?? works only if the type can be null. You should rewrite the condition so it doesn't use ?? and you'll be all set.You're also missing the condition where new event ends after m.end but starts after m.start and where newEvent starts and ends within the models interval (if that is possible in your model)If this helps, please take time to accept the solution. Thank you. 这篇关于检查日期是否重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 16:06