ericpap
ericpap

Reputation: 2937

Simple LINQ Order by not working

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!

Upvotes: 4

Views: 2501

Answers (2)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

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.

Upvotes: 10

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

it should be this way:

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

Upvotes: 2

Related Questions