Entity framwwork date in serivce

rashmitha r 21 Reputation points
2022-07-05T11:18:14.29+00:00

I want check the event of particular date, but while am checking

var leaveApply = _leaveApplyRepository.Table.Where(x => !x.Deleted && x.EmployeeId == empID && (x.LeaveFromdate >= FromDate && x.LeaveFromdate <= ToDate) && (x.LeaveTodate >= FromDate && x.LeaveTodate <= ToDate) && (x.LeaveFromdate == Date)).OrderBy(x => x.LeaveTypeId).ToList();

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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,078 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 31,756 Reputation points Microsoft Vendor
    2022-07-06T04:28:02.78+00:00

    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

    0 comments No comments