Sort lists in C #

0

I need to return the list facturas ordered by the field entidades .

I've tried like that but it does not work for me:

facturas.OrderBy(f => f.Entidades).ToList();

Code:

public ActionResult Index()
{
    var facturas = db.Facturas
        .Include( f => f.Entidades )
        .Include( f => f.Estados )
        .Include( f => f.FormasPagos )
        .Include( f => f.Numeraciones );


    return View( facturas.ToList( ));
}
    
asked by Jhon Castrillon 01.06.2017 в 16:28
source

3 answers

1

I suspect that the problem is that you expect the collection to be ordered in facturas . OrderBy as well as other LINQ methods return a new collection, they do not act on the original collection. Try with:

var facturasOrdenadas=facturas.OrderBy(f => f.Entidades.Nombre).ToList();

And use facturasOrdenadas if you want to manage the ordered collection.

    
answered by 01.06.2017 / 16:36
source
1

If you want to update it on it you have to use Sort

facturas.Sort(f => f.Entidades).ToList();

Source: link

    
answered by 01.06.2017 в 16:43
1

In theory OrderBy can be assigned to everything that returns a collection, so it can be put at the end of the collection invoices. the subject is in the parameter by which it is sorting, because Entities do not represent an attribute of the invoice class but a table that must have its own attributes. In summary, the consultation can be like this:

 var facturas = db.Facturas
    .Include( f => f.Entidades )
    .Include( f => f.Estados )
    .Include( f => f.FormasPagos )
    .Include( f => f.Numeraciones ).OrderBy(f => f.Entidades.Nombre);
    
answered by 01.06.2017 в 16:59