Comparison of Dates c#

Asked

Viewed 217 times

1

My question is this:.

inserir a descrição da imagem aqui

I need to display 2 status on a page that would be Primeiro and Alterado, where the first sql line with the date of 2018-03-05 10:24:55.787 get the status Primeiro and the second sql line with the date 2018-03-05 10:30:09.727 get the status Alterado and the third line gets the status Ativo but it is already working as you can see in the code below.

<td class="text-center">
  @if (item.StatPree == 1)
  {
      <a class="ui teal label">Ativo</a>
  }
  else {
      if (...)
      {
          <a class="ui yellow label">Primeiro</a>
      }
      else
      {
          <a class="ui orange label">Alterado</a>
      }
  }
</td>

The variable is like public DateTime? DataEdicao { get; set; }

  • q bank you use ?

  • also show where your item comes from, probably a list of items

  • I use the Sqlserver

  • Have you thought about using another code that differentiates the "First", as -1?

  • need to give more details of the data, as they come... at first, thinking it will return the data as they are in the database, just sort, take the first item as "First" and the last as "Changed"

  • What do you mean? You can give me an example?

  • 1

    If your data is in a Listby name dados, could do so:var primeiro=dados.Where(a => a.StatPree == 0).OrderBy(a => a.DataEdicao).FirstOrDefault(); var alterado = dados.Where(a => a.StatPree == 0).OrderBy(a => a.DataAlteracao).LastOrDefault();

  • @Leonardomacedo maybe in the same place where you make the differentiation from 0 to 1 (an update method, for example), you use another code for when to display the value "First".

  • So in statpree I can’t touch it because it’s a system audit page

  • @Ricardopunctual I’m testing your function you sent me

  • @Ricardopunctual My data is inside a foreach (var item in Model) in this case would be correct Model.Orderby?

  • @Rovannlinhalis published the answer with the same solution I talked about, just a little different already making the comparison. good luck

  • @Ricardopunctual, you gave an example by selecting a record within the list, in the first, ok, but in the changed, it can have X changes, it would not work the way you said. It would have to be at least a Tolist() at the end. Besides, it would have to change the logic of the loop, to display the data from two different sources.

  • 1

    @Rovannlinhalis I’m not criticizing your answer, I just answered Leonardo’s comment about him using the foreach, then I saw that you had answered and commented pq no longer need to give examples because your answer already answered his question :)

Show 9 more comments

1 answer

1


You can compare the date, and if the date equals the lowest date of the same Patientid, is the first, if not, is change:

Example:

if (item.DataEdicao == Model.Where(x=> x.PatientId == item.PatientId).Min(x=> x.DataEdicao))
{
  <a class="ui yellow label">Primeiro</a>
}
else
{
   <a class="ui orange label">Alterado</a>
}
  • 1

    It worked Buddy, thank you so much for your help!

  • for nothing, have

Browser other questions tagged

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