.Orderby() Does not order correctly - Aspnet Core

Asked

Viewed 50 times

-3

In an Aspnet Core 2.2 application, I had the following code that selected the field of a table and instructed to sort by Text:

Controller:

            //Bairros
            ViewBag.Bairros = _context.Imoveis.Select(c => new SelectListItem()
            { Text = c.Bairro, Value = c.Bairro }).OrderBy(x => x.Text).Distinct();

View

    <div class="form-group">
      <select name="bairro" asp-items="@ViewBag.Bairros" class="form-control">
         <option value="0">Todos os bairros</option>
      </select>
    </div>

In this previous version the code worked. When passing to version 3.1, the code does not work, it is as follows:

inserir a descrição da imagem aqui

Ref the versions,

Previous: Entityframeworkcore 2.2.8, Pomelo.EntityFrameworkCore.Mysql 2.1.0 Current: Entityframeworkcore 3.1.10, Pomelo.EntityFrameworkCore.Mysql 3.2.4

Does the version update have effect on the querie?

  • In good, that’s why the community is as it is. I have asked the questions at EO in English and the crowd help well. The moderating "Know-it-alls" here not only do not help, they deny the question. I did not ask at random, I put as much information as possible. I know it’s not all of them, but I’m sorry. It has to sink anyway.

  • 3

    Reversed in such a way that I don’t even know where to start. First of all, voting is secret, I find it strange to say who was or wasn’t. According to which one has something to question, it would be more appropriate in the [goal]. Third, it is irrelevant whether you asked at random or not, because motivation to post is not a criterion for voting, but content. Fourth, about having put as much information as possible, it is far from it. You need to learn how to do one [mcve] in the next (the link teaches). About the "have to sink", it goes of the commitment of the users. Just does not sink more pq fortunately we have moderators and users who vote right.

  • 3

    PS: today I read the post tb negativei, but by mere indexing of content, nothing personal (you did the [Tour] right? there explains). I closed the question provisionally to avoid "the site sink", but if you read the documentation (that is not my invention), will surely find correct, because you have learned that to have a better site, just [Edit] and improve the post, that it is automatically revised by the whole community, not only by moderation. And it is democratic, via vote. That is, if the moderator made a mistake, surely others will reopen and be positive. If it doesn’t, well, then the real problem is the question...

  • 2

    @Bacco I at the time made this unfortunate comment due to a frustration, and here I apologize to the community. Hj I see that the community needs help from experienced people. Being a programmer involves many facets, and there are N ways to start programming. I did read almost all the info on Meta, on how to ask clear and objective questions and today I avoid asking questions, but I try to help according to the level I have and even encouraging negative people to read the Meta to ask better questions, since we are here to help and not make code for others but vlw the touch

  • 3

    I’m glad you reviewed a few points. Lately when I do counterpoint, I imagine that will come "stoned" back (so much so that I wrote reluctantly if I should or not). I hope you see moderators as normal people who are trying to accomplish a goal (and who also sometimes manifest themselves when they feel wronged, as I did here), but that the ultimate goal is always to produce positive results. Thanks for the cordial feedback, it’s rare to see it here. And always consider votes and closures as mere organization, and not "like" or "dislike".

  • 3

    I thank you, in the end we are all here learning. And let’s move on ✌

Show 1 more comment

1 answer

1


After researching I made some changes and Orderby() worked.

The previous Code:

    ViewBag.Bairros = _context.Imoveis.Select(c => new SelectListItem()
    { Text = c.Bairro, Value = c.Bairro }).OrderBy(x => x.Text).Distinct();

The redone code:

ViewBag.Bairros = _context.Imoveis.Select(c => new SelectListItem()
    { Text = c.Bairro, Value = c.Bairro }).Distinct().OrderBy(a => a.Value);

Browser other questions tagged

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