为什么IEnumerable.ToList()以字母顺序返回字段? [英] Why IEnumerable.ToList() return fields in alphabetical order?

查看:166
本文介绍了为什么IEnumerable.ToList()以字母顺序返回字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用LINQ投影返回IENumerable Colection的类中具有此简单函数:

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();                
            }
        }

有人可以告诉我为什么IEnumerable的字段以字母顺序而不是原始对象定义返回吗?以及如何按特定顺序返回成员?谢谢

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

更新 对不起,如果我的问题不清楚.我会尽力解释我的问题. Pedidos类(实际上是从EF生成的POCOs类)具有以下一些属性:

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; }        
    }

我的订单问题与类中的数据无关,因此ORDER建议不能解决我的问题.这是关于属性的顺序.当我使用ToList()来填充此类时,我会按字母顺序(Aprobado,Bruto等)获得属性,而不是按类顺序的定义(ID_Pedido,Numero等...).字段名称的语言在这里无关紧要.希望这清楚我的答案.

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.

显示的结果

当我尝试通过数据网格在cliente上显示数据时:

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?

推荐答案

就像SQL一样,LINQ查询没有定义的顺序.它可以按任何想要的顺序返回.它返回有序列表的事实可能是由于内部工作原理,因此不应该依赖它.如果要建立特定的订单,请使用orderbyOrderBy方法:

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();  

这篇关于为什么IEnumerable.ToList()以字母顺序返回字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆