Creating a foreach as a result of an Entity query

Asked

Viewed 94 times

0

Hello, I’m trying to foreach a query result, the field I need is a string, but at the time I step into the foreach it’s being transformed into another value.

I need to compare two dates, in case the conversion of the field String to Datetime is giving error, because as mentioned above the field is being modified at the time it passes in foreach.

Below is the code in Pagemodel:

var tp = _context.TarifasPrecosItens.Where(p => p.TarifasPrecosId == tx.Id).First();

foreach(var hora in tp.De)
{
    DateTime horaInicial = Convert.ToDateTime(hora);
    DateTime horaCliente  = Convert.ToDateTime(totalPermanencia);

    if (horaCliente > horaInicial)
    {
        PagamentoTarifaUsada = tx.Descricao + " Acima de " + hora;
        Tarifa = tp.Valor;
        Total = tp.Valor;
    }
} 
  • 1

    What is Of? is a list? if yes, what is it? the error happens in which of the two conversions of foreach? where does the variable come from totalPermanencia?

  • 1

    De is a field that comes from the table, in case it has a value of "01:00" only after it passes to the foreach it is transformed into "48" ai in case the conversion error.

  • 1

    var tp = _context.TarifasPrecosItens.Where(p => p.TarifasPrecosId == tx.Id).First(); You’re not just picking up a record?

  • 2

    Sorry... I don’t understand what you want to do, if the "1:00" field and you pass it by foreach, will return "0", "1", ":", "0", "0", ie, will return each char separate, See here

  • Luke, in case tp receives several records from the table, in case tp. From and one field, however in has several values returned from the query.

  • Barbertta, truth you are right, I need to turn into a list before moving to the foreach, but also need to take the value of another field, in the position where stop in the if. You know how I can do that?

  • 1

    @mba, write your question the way you want to solve your problem. You already know that the foreach is not the problem you want to solve, but rather catch properties of TarifasPrecosItens. Which ones items of TarifasPrecosItens? Which ones estates? What you want to do with them? Why they will be used?

Show 2 more comments

2 answers

0

I believe - you see, I I believe, because, by description, you can’t understand the problem - that what you want to do is the following:

var tp = 
    _context
        .TarifasPrecosItens
        .Where(w => Convert.ToDateTime(w.TotalPermanencia) > Convert.ToDateTime(w.De)); 

foreach (var item in tp)
{
    item.PagamentoTarifaUsada = $"{item.Descricao} acima de {item.De}";
    item.Tarifa = item.Total = item.Valor;
}

_context.SaveChanges();
  • 1

    It speaks @Marcelo Uchimura, as you caught the conversation walking there became kind of strange anyway. But the initial doubt was yes on the foreach, because it was changing the value, but as you said yourself I already solved, I did not mark Barbertta’s response as a response because it was a comment. But I will be deleting this post so that no one else tries to resolve this issue. Thank you for the answer. Hugs.

0


I didn’t answer before because I was too busy.

The problem is because you are returning a single item of "01:00" value and passing this value by foreach will return each char of string, you can see an example here.

To return a list, remove the First()

var tp = _context.TarifasPrecosItens.Where(p => p.TarifasPrecosId == tx.Id);

Another thing I would do is use TryParse to make the conversion, as it is possible to make a treatment if any error occurs, you can use it more or less like this:

foreach (var item in tp)
{
    DateTime horaInicial;
    if (!DateTime.TryParse(item.De, out horaInicial))
    {
        //Tratamento erro
    }

}

From C# 7 it is possible to simplify the TryParse as follows

DateTime.TryParse(item.De, out DateTime horaInicial)

Browser other questions tagged

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