LINQ simple order not working

I am new to LINQ. I am using this function:

public IEnumerable<Vendedores> GetVendedores()
    {
        using (var context = new OhmioEntities())
        {
            Vendedores _allvendors= new Vendedores();
            _allvendors.Nombre = "All Vendors";
            _allvendors.ID_Vendedor = -1;
            var query = context.Vendedores;
            var _vendors= query.Where(f => f.Activo == true).OrderBy(o=>Nombre).ToList();                                    
            _vendors.Insert(0, _allvendors);
            return _vendors;
        }
    }

He should give me a list of orders from active suppliers. Where the part works fine, but the order is ignored, and the entries after the .ToList are in the original order of the table. What am I doing wrong? Thank!

+4
source share
2 answers

I think you need o.NombreinsteadNombre

var _vendors = query
              .Where(f => f.Activo)
              .OrderBy(o=> o.Nombre)
              .ToList();          

It f => f.Activo == truecan also be written as f => f.Activo.

+10
source

It should be as follows:

var _vendors= query.Where(f => f.Activo == true).OrderBy(o=>o.Nombre).ToList();
+2
source

Source: https://habr.com/ru/post/1536474/


All Articles