Hi @rashmitha r ,
Here (x.LeaveFromdate == Date)) it checks with date ad time value, how can I remove time while checking with database data. I want o check 2022-07-02 == 2022-07-02
You can remove the DateTime's time value via the DateTime.ToString Method or DateTime.ToShortDateString Method, but after that, the return value will be a string type, like this:
var FromDate = new DateTime(2022, 7, 1, 10, 0, 0); //result: 7/1/2022 12:00:00 AM
var datewithouttime = FromDate.ToString("yyyy-MM-dd"); //result: datewithouttime = "2022-07-01"
Since you want to filter value based on the date range, I think you have to change it back to the datetime type. In this scenario, you can try to use DateTime.ParseExact() method to convert the date string to the datetime type. [Note] To the end date, you can add one day. Code like this:
var ToDate = new DateTime(2022,7, 3, 18, 0, 0, 0); //ToDate = {7/3/2022 6:00:00 PM}
var newToDate = DateTime.ParseExact(ToDate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null).AddDays(1); // newToDate = { 7 / 4 / 2022 12:00:00 AM}
Then, you can change your query string like this:
var leaveApply = _leaveApplyRepository.Table.ToList().Where(x => !x.Deleted && x.EmployeeId == empID &&
(DateTime.ParseExact(x.LeaveFromdate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null) >= DateTime.ParseExact(FromDate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null) &&
DateTime.ParseExact(x.LeaveFromdate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null) <= DateTime.ParseExact(ToDate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null).AddDays(1)) &&
(DateTime.ParseExact(x.LeaveTodate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null) >= DateTime.ParseExact(FromDate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null) &&
DateTime.ParseExact(x.LeaveTodate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null) <= DateTime.ParseExact(ToDate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null).AddDays(1)) &&
(DateTime.ParseExact(x.LeaveFromdate.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null) == DateTime.ParseExact(Date.ToString("yyyy-MM-dd"), "yyyy-MM-dd", null))).OrderBy(x => x.LeaveTypeId).ToList();
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion