Recording date range, with usable days in Dictionary

0

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

1) Write a% c and% in C #, a range of dates. For this I have a function that I use in the SQLServer here of the company, which returns me an integer of working days. The architecture pattern is already set 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 workdays in an integer.

2) After storing the dates in dictionary<T> , I need to read, check the dates.

Then it 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!

    
asked by anonymous 31.07.2018 / 01:36

1 answer

0

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

Follow the 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 that the return of the bank is amount of working days, I passed this in the method parameter, the method DatasUteis1 and DatasUteis3 skip the weekends and consider only the exact amount of days passed as a parameter, already the DatasUteis2 and DatasUteis4 methods consider the total amount of working days reported.

I also put it in .NET Fiddle for reference.

    
31.07.2018 / 04:18