Help with Daysofweek method in Asp . net

Asked

Viewed 22 times

-1

I am one creating an api using Asp.Net that performs task listings and these tasks are displayed per day, week, month and year, my doubt is on how to perform this call only for the current week that I am, I am not able to do this way below, I would like help.

    [HttpGet("filter/week/{macAddress}")]
    public async Task<IActionResult> Week(string macAddress)
    {
        try
        {
            var result = await _repo.GetTodosItens(macAddress);

            var week = result.AsQueryable<TaskModel>().Where(t => t.When.DayOfWeek == DateTime.Now.DayOfWeek);

            return Ok(week);
        }
        catch (Exception ex)
        {
            return BadRequest($"Erro: {ex.Message}");
        }
    }
  • What is returned by doing it this way? ( What you expected/ What you get)

  • If you’re gonna do the DayOfWeek on both sides, can use only the Day. You’re probably having trouble converting this to SQL.

1 answer

0

I was doing wrong, with the help of a co-worker, he passed me the following method:

    private static int GetWeekNumber(DateTime date)
    {
        System.Globalization.CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentCulture;
        System.Globalization.Calendar currentCalendar = currentCulture.Calendar;

        return
            currentCalendar.GetWeekOfYear(date,
                                          currentCulture.DateTimeFormat.CalendarWeekRule,
                                          currentCulture.DateTimeFormat.FirstDayOfWeek);

    }

With it it is possible to pass the date and it returns the week that I am, with this I can compare with the current week

Browser other questions tagged

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