简体   繁体   中英

Why IEnumerable.ToList() return fields in alphabetical order?

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.

这是VS的结果

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?

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 or OrderBy method:

               (from Pedidos in context.Pedidos
                join Clientes in context.Clientes on Pedidos.ID_Cliente equals Clientes.ID_Cliente
                where Pedidos.ID_Comprobante == sComprobante
                orderby Pedios.[the property you want to order by]
                select Pedidos).ToList();  

The order in a IEnumerable is not defined, and it could be any thing. If you still want to define an order, you can use OrderBy .

There's nothing in your query that is intentionally ordering it alphabetically.

Use the orderby keyword to order the results:

from Pedidos in context.Pedidos
join Clientes in context.Clientes
    on Pedidos.ID_Cliente equals Clientes.ID_Cliente
where Pedidos.ID_Comprobante == sComprobante
orderby Pedidos.ID  // change to the field you want to sort by alphabetically
select Pedidos

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM