skip to Main Content

I want to get all dates in the last three months, so I did the following:

protected void BindPermissions(int empNum)
        {
            var permPeriod = new Dictionary<int, int>();
            permPeriod.Add(DateTime.Now.Year, DateTime.Now.Month);
            permPeriod.Add(DateTime.Now.AddMonths(-1).Year, (DateTime.Now.AddMonths(-1).Month));
            permPeriod.Add(DateTime.Now.AddMonths(-2).Year, (DateTime.Now.AddMonths(-2).Month));
            var dt = payload_object.AttendancePermissionBO.permissionList
                .Where(x => x.empNum == empNum
                && ((x.permDate.Year == permPeriod.Keys.ElementAtOrDefault(0) && x.permDate.Month == permPeriod.Values.ElementAtOrDefault(0)) ||
                (x.permDate.Year == permPeriod.Keys.ElementAtOrDefault(1) && x.permDate.Month == permPeriod.Values.ElementAtOrDefault(1)) ||
                (x.permDate.Year == permPeriod.Keys.ElementAtOrDefault(2) && x.permDate.Month == permPeriod.Values.ElementAtOrDefault(2)))).ToList().OrderBy(x => x.permDate);
            GV_PermissionHistory.DataSource = dt;
            GV_PermissionHistory.DataBind();
        }

Is there a better way to do that or this method suits what i need?

3

Answers


    1. Get the date (boundary) three months ago from today.
    DateTime threeMonthsAgoDate = DateTime.Now.AddMonths(-3);
    
    1. Filter the data for permDate that after (inclusive) the date from 3 months ago.
    var dt = payload_object.AttendancePermissionBO.permissionList
        .Where(x => x.empNum == empNum
            && x.permDate >= threeMonthsAgoDate.Date)
        .OrderBy(x => x.permDate)
        .ToList();
    

    Updated:

    This answer is for querying records from the last 3 months ago until the current date.

    Based on Post Owner’s requirements and existing code, what he needs is from

    Start Date: 2022-05-01

    End Date: 2022-07-31 (inclusive)

    Hence @Astrid’s answer is most accurate.

    Login or Signup to reply.
  1. You can use the below code for all the dates of last X- months. You can do some changes based on your requirements.

     public static void Main()
        {
            var list= new List<DateTime>();
            
            for(int i=0;i<3;i++){
                var month= DateTime.Now.AddMonths(-i);
                var monthDates= GetDates(month.Year, month.Month);
                
                list.AddRange(monthDates);
            }
            
            foreach(var item in list){
            Console.WriteLine(item.ToString());
            }
        }
        
    public static List<DateTime> GetDates(int year, int month)
    {
       return Enumerable.Range(1, DateTime.DaysInMonth(year, month))  
                        .Select(day => new DateTime(year, month, day)) 
                        .ToList(); 
    }
    
    Login or Signup to reply.
  2. It seems like you do not really want all dates in the last three months, but you want all items in your collection where permDate is a date within some date range.

    Given your own approach, that some date range seems to be the two previous months plus the entire current month. I.e. for 5th of July 2022, the date range is all of May, June and July 2022.

    I think you can simplify your approach by defining a start date and an end date, and compare the permDate values with those two values. A straight-forward way of doing that could be:

    var today = DateTime.Today;
    
    var startMonth = today.AddMonths(-2);
    var endMonth = today.AddMonths(1);
    
    var startDate = new DateTime(startMonth.Year, startMonth.Month, 1);
    var endDate = new DateTime(endMonth.Year, endMonth.Month, 1);
    

    Then, you can use startDate and endDate in your filtering:

    var dt = payload_object.AttendancePermissionBO.permissionList
        .Where(x => 
            x.empNum == empNum &&
            x.permDate >= startDate &&
            x.permDate < endDate)
        .OrderBy(x => x.permDate)
        .ToList();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search