Problem with database query by date using Line and Entity in C#

Asked

Viewed 90 times

1

I have the following appointment at the bank;

consultas = ctx.Consultas.Include(c => c.Cliente).Include(p => p.Procedimento).ToList();

which works perfectly, but I want to include a Where to filter by scheduling date, I have tried some ways as:

consultas = ctx.Consultas.Where(a => a.Inicio.Date.ToString("yyyy-MM-dd") == entrada).Include(c => c.Cliente).Include(p => p.Procedimento).ToList();`

or

   consultas = ctx.Consultas.Where(a => a.Inicio.Date == DateTime.Parse(entrada)).Include(c => c.Cliente).Include(p => p.Procedimento).ToList();`

as well as other variations like this, however my problem persists, a return of an empty list, this field in my database (mysql) is in the following format 2019-05-14 12:30:00 (yyyy-MM-dd HH:mm:ss), my query should disregard the time, I have tried to pass the input both as string and also datetime

  • Already used Dbfunctions.Truncatetime?

  • Hello Lucas this field Inicio is a Datetime ?

2 answers

1


Lucas come on let’s see if I can help you.

consultas = ctx.Consultas.Where(a => a.Inicio.Day == entrada.Day && a.Inicio.Month == entrada.Month && a.Inicio.Year == entrada.Year).Include(c => c.Cliente).Include(p => p.Procedimento).ToList();
  • Entrance is a string, has not Day, Month or Year. Try to avoid unnecessary comment at the end of the answer. If you get a negative vote, it’s probably because there’s something wrong with the answer (which is the case now). In this case, review what was posted.

  • 1

    for now was the only one that worked

  • Hello Lucas how good it worked for you. That’s why I still enter the community, to be able to help others. @LINQ glad it worked and I wasn’t negative. Good programming for all of us.

0

Alternative 1:

consultas = ctx.Consultas.Where(a => a.Inicio.Date == entrada.Date)
.Include(c => c.Cliente).Include(p => p.Procedimento).ToList();

Alternative 2:

consultas = ctx.Consultas.Where(a => a.Inicio.Date.ToString("yyyy-MM-dd") == entrada.ToString("yyyy-MM-dd"))
.Include(c => c.Cliente).Include(p => p.Procedimento).ToList();

Browser other questions tagged

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