Orderby lambda keeps changing with every search

Asked

Viewed 115 times

3

I have a search in lambda, in which refers information of a grid, but at each different query it changes the information of place:

Indicadores = (Pegatodos().Where(f => f.STATUS == "A")
                       .Select(x => new 
           { SEQINDICFINANC = x.SEQ, DESCRICAO = x.DESCRICAO, SIGLA = x.SIGLA })
                         .OrderBy( x => x.SEQ)
                         .ToList())

it returns the main information of my grid:

NOME  datas    |   datas |  datas
seq    0           0         0
seq    0           0         0
seq    0           0         0
seq    0           0         0

but for each consultation of different date periods the position of the seq keeps changing, and I need you to stay in the same position.

  • Check the type of your SEQ attribute.

  • virtual type long in C# and number in BD

  • Can you please put in your question the code of Pegatodos()?

  • I am using the Entity framework, in which case the default Return Return enditades does not know exactly the code

2 answers

2


The change was occurring in the script even the same ordering done in C# I used in JS and it worked, Thanks

success: function (result) {
              var dataArray = [];
              var valarray = [];
              var datas = {}; 
              var total = 0;

              for (var j = 0, length = result.Datas.length; j < length; j++) {
                  var indicador = {};

                  for (var l = 0; l < result.Indicadores.length; l++) {
                      if (result.Datas[j].PKINDIC== result.Indicadores[l].PKINDIC) {
                          indicador.pkindic= result.Datas[j].PKINDIC;
                          indicador.descricao = result.Indicadores[l].DESCRICAO;
                          indicador.valor= result.Datas[j].VALOR;
                          indicador.dtas = result.Datas[j].DTA;
                          indicador.pkvari= result.Datas[j].PKVARI;

                          dataArray.push(indicador);
                      }
                  }
              }

              //#region BubbleSort DataArray

              for (var cont = 0; cont < dataArray.length; cont++) {
                  for (var cont2 = 0; cont2 < dataArray.length - 1; cont2++) {
                      if (dataArray[cont2].dtas > dataArray[cont2 + 1].dtas) {
                          var temp = dataArray[cont2 + 1];
                          dataArray[cont2 + 1] = dataArray[cont2];
                          dataArray[cont2] = temp;
                      }
                  }
              }

              //#endregion

After taking all the results in 2 different arrays and moving to a single one with the information I needed, I used a bubblesort method to sort it out so, what did not have before and in the transformation of 2 arrays to 1 was reordering the information randomly and with this method after the transformation created a fixed ordering.

People thanks for the help, if anyone has any similar problem put here

1

Just reverse the OrderBy with the Select. The return of Select does not guarantee an ordered list:

Indicadores = (Pegatodos().Where(f => f.STATUS == "A")
              .OrderBy(x => x.SEQ)
              .Select(x => new { 
                  SEQINDICFINANC = x.SEQ, 
                  DESCRICAO = x.DESCRICAO, 
                  SIGLA = x.SIGLA })
              .ToList())
  • Keep switching, with each search =/ Indicators = (Pegatodos(). Orderby(x => x.SEQ) . Where(f => f.STATUS == "A") . Select(x => new { SEQINDICFINANC = x.SEQ, DESCRIPTION = x.DESCRIPTION, ACRONYM = x.ACRONYM }) . Tolist())

  • Just so I know where these dates come from?

  • dates, comes from parameter of the method that is called a view and invoked by ajax, in case the user who chooses the period, explained right?

  • It is that I see no correlation with what is being executed. So my doubt. It may be another factor that is changing the order records.

  • well, the information goes back to a script that uses Indicators as a basis, and relates other information to generate the lines without reorganizing or changing the Indicators, just search for information using it as a basis, the strange thing is that this 'bug' only occurs when there are more than 3 records, I’ll debug here, and if I find an answer I post, thank you

  • is really changing the script , but in c# it is not ordering, the values of seq are scrambled I will try to sort by JS

Show 1 more comment

Browser other questions tagged

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