I have this simple function in a class that return IENumerable Colection using LINQ projection:
public IEnumerable<Pedidos> Pedidos_Listar(string sComprobante, Clientes MyCliente = null, DateTime? dDesde = null, DateTime? dHasta = null, bool bCumplidos = false)
{
using (var context = new OhmioEntities())
{
return
(from Pedidos in context.Pedidos
join Clientes in context.Clientes on Pedidos.ID_Cliente equals Clientes.ID_Cliente
where Pedidos.ID_Comprobante == sComprobante
select Pedidos).ToList();
}
}
Can anybody tell me why the fields of the IEnumerable are returned in alphabetical order instead of the original object definition? And how do I return members in a specific order? Thank you
UPDATE Sorry if my question wasn't clear. i'll try to explain my problem. The class Pedidos (It's realy a POCOs class generated from EF) has some properties like this:
public class Pedidos
{
public virtual int ID_Pedido { get; set; }
public virtual int Numero { get; set; }
public virtual DateTime Fecha { get; set; }
public virtual DateTime FechaEntrega { get; set; }
public virtual string Cliente { get; set; }
public virtual Decimal Bruto { get; set; }
public virtual Decimal Neto { get; set; }
public virtual Boolean Aprobado { get; set; }
public virtual string Observaciones { get; set; }
public virtual Boolean Entregado { get; set; }
}
My order problem is not about the data inside the class, so the ORDER sugestion doesn't resolve my problem. It's about the order of the properties. When i use ToList() to fill this class i get the properties in alphabetical order (Aprobado, Bruto, etc...) instead of the definition of the class order(ID_Pedido, Numero, etc...). The language of the names of the fields is irrelevant here. Hope this clear my answer.
When i try to show the data on cliente over a datagrid:
As you can see on both cases properties properties appear order alphabelicaly instead the class order. Why?
There's nothing in your query that is intentionally ordering it alphabetically.
Use the
orderby
keyword to order the results:Just like with SQL, there is no defined ordering for LINQ query. It can return in any order it wants. The fact it returns ordered list is probably thanks to internal workings and should not be relied upon. If you want to build specific order, use
orderby
orOrderBy
method:The order in a
IEnumerable
is not defined, and it could be any thing. If you still want to define an order, you can useOrderBy
.