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;
}
}
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?– Barbetta
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.
– mba
var tp = _context.TarifasPrecosItens.Where(p => p.TarifasPrecosId == tx.Id).First();
You’re not just picking up a record?– Lucas Brogni
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– Barbetta
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.
– mba
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?
– mba
@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 ofTarifasPrecosItens
. Which ones items ofTarifasPrecosItens
? Which ones estates? What you want to do with them? Why they will be used?– Marcelo Shiniti Uchimura