Select days comparison with current date does not work

Asked

Viewed 128 times

-2

I have this select which brings back the data as follows, if DataCobranca for 10, and today is day 05, it will pick up all customers that is day 10, as it will be generated 5 days before, or case DiaEspecificoMarcar for true and DiaEspecifico is the same as today, it will also appear in this select

var contrato = db
    .Contrato
    .Include(a => a.PedidoVenda)
    .Include(a => a.Cliente)
    .Where(a => a.PedidoVenda.EmpresaID == model.EmpresaID && 
                a.Cancelado == false && 
                DateTime.Parse(a.PrimeiraCobranca.ToString("dd/MM/yyyy")) >= DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy")) &&
                a.DiaEspecificoMarcar == true && a.DiaEspecifico == int.Parse(DateTime.Now.ToString("dd"))) ||
                (a.DiaEspecificoMarcar == false && (int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota) == int.Parse(DateTime.Now.ToString("dd")))
    ).ToList();

Only when the DataCobranca for 05, it needs to be generated 5 days before, or as long as it is in the parameter, so as I am using retroactive date, on 31/03 and on 01/04, where I should pull those of day 05, but it always pulls zeroed.

In fact, because the DataCobranca = 5, and the DataProcessamentoNota = 5, as he does the account, he checks 5-5 = 0, in case he should take the last day of the month, or else if DataProcessamentoNota = 10 it should do the count 5 - 10, and then it would be -5. The result should be 10 days earlier. How to calculate as days same. How can I verify?

  • 1

    DateTime.Parse(a.PrimeiraCobranca.ToString("dd/MM/yyyy")), what is this crazy parse? the first Obranca is not a datetime? type and also what makes it nothing? See problems in your code

  • @Virgilionovic I need to check if the date of the first charge is greater than or equal to the day being generated. is yes, I’m only getting the date, not the time.

  • @Virgilionovic until then when it doesn’t get negative works, I need to do the part of int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota which is the one that’s giving problems when it generates a negative, because it’s being treated as int and not DateTime

  • 1

    Mariana a.Primeiracobranca is the type DateTime? What is the version of EntityFramework? if it is not how you do it !

  • 1

    Yeah, it’s kind of DateTime, the version of Entityframeworkcore is 2.1.4

  • your conversions are all wrong! that’s the problem too, and another subject generates conversions at the customer level is bad this search.

  • Here you need to catch the day? a.DataCobranca.Substring(1, 2) ???? the correct is a.DataCobranca.Day internally he makes the conversion.

  • In case the DataCobranca is a string type, it saves T05, T10, etc, so I use the Substring to get both numbers.

  • 1

    where you did it: DateTime.Parse(a.PrimeiraCobranca.ToString("dd/MM/yyyy")) that’s all a.PrimeiraCobranca.Date.

  • 2

    int.Parse(DateTime.Now.ToString("dd")) it’s just DateTime.Now.Day

  • then this can stay as it is! DataCobranca

  • I’ll give you a hint! Do a search with type 10 only and then do one with type 5, narrow it down that way. If you need to put these two together at the end it’s easy, but the way you’re very confused understand until your question ... !!!

  • @Virgilionovic but until then does not solve my problem, since the problem is here int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota, what you gave me improves the code, but it doesn’t solve the problem.

  • The code improvement was good for you to see the wrong path you were taking, but, uh, along that line int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota has another problem, but, tell me what’s wrong with this line?

  • The problem is date, he’s comparing to DataCobranca = T10 with the DataProcessamentoNota = 5, or by taking the Substring 10 - 5 = 5 he would know that to generate the note on day 5, now when it is smaller, for example DataCobranca = T05 and DataProcessamentoNota = 05 05-05 he would give 0, or 05-10 = -5, which he will compare with the current day, to see if it is the correct day, in case 05-05, he should generate on the last day of the previous month, and 05-10, he should generate on day 26. I don’t know if you understand.

Show 10 more comments

1 answer

1


It can be done this way

var contrato = db
.Contrato
.Include(a => a.PedidoVenda)
.Include(a => a.Cliente)
.Where(a => a.PedidoVenda.EmpresaID == model.EmpresaID && 
            a.Cancelado == false && 
            DateTime.Parse(a.PrimeiraCobranca.ToString("dd/MM/yyyy")) >= DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy")) &&
            a.DiaEspecificoMarcar == true && a.DiaEspecifico == int.Parse(DateTime.Now.ToString("dd"))) ||
            (a.DiaEspecificoMarcar == false && ((int.Parse(a.DataCobranca.Substring(1, 2)) 
    - a.PedidoVenda.Empresa.DataProcessamentoNota)<=0?DateTime.Now.Date.AddDays(int.Parse(a.DataCobranca.Substring(1, 2)) 
    - a.PedidoVenda.Empresa.DataProcessamentoNota).Day:int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota) == int.Parse(DateTime.Now.ToString("dd")))
).ToList();

Browser other questions tagged

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