Recording date range, with useful days in Dictionary

Asked

Viewed 110 times

0

I need to build two methods in my system, which do the following:

1) Save to a dictionary<T> in C#, a crease of dates. For this I have a function that consumption in the Sqlserver here from the company, who returns me a whole working day. The standard architecture is already established this way, so I have to do it this way;

The Function of Sqlserver, I consume as follows:

using (IDbConnection dbConnection = Connection)
{
    string sQuery = "select dbo.working_days(@dtUtil) as QuantidadeDiasUteis";
    dbConnection.Open();

    return dbConnection.Query<DiasUteis>(sQuery, new { dtUtil = 
    Convert.ToDateTime(DtDiaUtil) }).FirstOrDefault();

}

Note: My input parameter is a future date, and it returns me the number of working days in an integer.

2) After the dates in the Dictionary, I need to read, check the dates.

So the structure would look something like this:

31/07/2018 = 1;
01/08/2018 = 3;
06/08/2018 = 4; ==> (Ignorou os Sábados e Domingos);

I need to return this information!

  • It has to be a Dictionary? since it will store only dates can not be a List<DateTime>?

1 answer

0

I made 4 options of methods for calculating dates, two with list and two with Dictionary

Follows Code

public static List<DateTime> DatasUteis1(int quantidade)
{
    List<DateTime> datas = new List<DateTime>();
    DateTime data = DateTime.Today;
    for (int i = 1; i <= quantidade; i++)
    {
        if (data.AddDays(i).DayOfWeek != DayOfWeek.Saturday && data.AddDays(i).DayOfWeek != DayOfWeek.Sunday)
            datas.Add(data.AddDays(i));
    }

    return datas;
}

public static List<DateTime> DatasUteis2(int quantidade)
{
    List<DateTime> datas = new List<DateTime>();
    DateTime data = DateTime.Today;
    for (int i = 1; i <= quantidade; i++)
    {
        if (data.AddDays(i).DayOfWeek != DayOfWeek.Saturday && data.AddDays(i).DayOfWeek != DayOfWeek.Sunday)
            datas.Add(data.AddDays(i));
        else
            quantidade++;
    }

    return datas;
}

public static Dictionary<int, DateTime> DatasUteis3(int quantidade)
{
    Dictionary<int, DateTime> datas = new Dictionary<int, DateTime>();
    DateTime data = DateTime.Today;
    for (int i = 1; i <= quantidade; i++)
    {
        if (data.AddDays(i).DayOfWeek != DayOfWeek.Saturday && data.AddDays(i).DayOfWeek != DayOfWeek.Sunday)
            datas.Add(i, data.AddDays(i));
    }

    return datas;
}

public static Dictionary<int, DateTime> DatasUteis4(int quantidade)
{
    Dictionary<int, DateTime> datas = new Dictionary<int, DateTime>();
    DateTime data = DateTime.Today;
    for (int i = 1; i <= quantidade; i++)
    {
        if (data.AddDays(i).DayOfWeek != DayOfWeek.Saturday && data.AddDays(i).DayOfWeek != DayOfWeek.Sunday)
            datas.Add(i, data.AddDays(i));
        else
            quantidade++;
    }

    return datas;
}

As you said the return of the bank is number of working days, I passed this in the method parameter, the method DatasUteis1 and DatasUteis3 skip weekends and consider only the exact amount of days passed as a parameter, whereas methods DatasUteis2 and DatasUteis4 consider the total number of working days informed.

I also put in the .NET Fiddle for reference.

  • Good morning! The logics are great and very easy to understand, but what I need is for each date I report as an input parameter, in the Sqlserver function, I return the date and how many working days have for it. Example:

  • Good morning! I really appreciate the help, The logics are great and very easy to understand, but what I need is for each date I inform as input parameter, in the function of Sqlserver, I return the date and how many working days have for it. Example: 01/08/2018, 1 02/08/2018, 2 03/08/2018, 3 04/08/2018, 3 05/08/2018, 3 06/08/2018, 4 Where: <Day Date>, <Qtd useful days>

  • In the Sqlserver function I pass as input parameter a range of dates <Starting Date>,<Final Date>. The question of using the dictionary was purposeful, as these returns will be used in a calculation function later in the project. So we don’t want every call in the calculation method to open a connection in the bank. To do so, we will run this once only at application startup, in a Singleton or other standard to start along with the application.

  • Doubt: 1) You report a future date and will always count from today until that future date, right? or the initial date could be tomorrow, for example? 2) in case the day 04 and 05 are weekends, they should be on the list?

  • I updated the Fiddle with method 5, in it if it is informed 7 working days, it will show the next 7 working days

Browser other questions tagged

You are not signed in. Login or sign up in order to post.