Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple LINQ Order by not working

Tags:

c#

linq

I'm new with LINQ. I'm 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;
        }
    }

It should give me order list of active vendors. The where part work fine, but the order is ignored and the records after the .ToList are in the original table order. What i'm i doing wrong? Thank you!

like image 895
ericpap Avatar asked Dec 14 '22 23:12

ericpap


1 Answers

I think you need o.Nombre instead of Nombre

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

Also f => f.Activo == true can be written as f => f.Activo.

like image 82
Selman Genç Avatar answered Jan 07 '23 15:01

Selman Genç