linq没有按顺序处理c#上的列表
我有这个密码linq没有按顺序处理c#上的列表,c#,linq,C#,Linq,我有这个密码 public void OrdenarPedidosPorFecha() { pedidos.OrderBy(pedido => pedido.fechaEntrega); } Plan planT = new Plan(); Producto productoT = new Producto("productoTest", 0, 0, 0); Cliente clie
public void OrdenarPedidosPorFecha()
{
pedidos.OrderBy(pedido => pedido.fechaEntrega);
}
Plan planT = new Plan();
Producto productoT = new Producto("productoTest", 0, 0, 0);
Cliente clienteT = new Cliente("clienteTest", 0);
Pedido pedidoT = new Pedido(productoT, clienteT, 0, DateTime.Now);
Producto productoT1 = new Producto("productoTest1", 0, 0, 0);
Cliente clienteT1 = new Cliente("clienteTest1", 0);
Pedido pedidoT1 = new Pedido(productoT1, clienteT1, 0, DateTime.Now.AddDays(1));
Producto productoT2 = new Producto("productoTest2", 0, 0, 0);
Cliente clienteT2 = new Cliente("clienteTest2", 0);
Pedido pedidoT2 = new Pedido(productoT2, clienteT2, 0, DateTime.Now.AddDays(2));
planT.AgregarPedidoAPlan(pedidoT2);
planT.AgregarPedidoAPlan(pedidoT);
planT.AgregarPedidoAPlan(pedidoT1);
MessageBox.Show(planT.pedidos[0].fechaEntrega.ToString());
MessageBox.Show(planT.pedidos[1].fechaEntrega.ToString());
MessageBox.Show(planT.pedidos[2].fechaEntrega.ToString());
planT.OrdenarPedidosPorFecha();
MessageBox.Show(planT.pedidos[0].fechaEntrega.ToString());
MessageBox.Show(planT.pedidos[1].fechaEntrega.ToString());
MessageBox.Show(planT.pedidos[2].fechaEntrega.ToString());
当它显示它们时,输出是相同的。你知道为什么不比较日期吗?我还尝试将.Date添加到fechaEntrega中,但它也不起作用。LINQ的
OrderBy
不会对源序列进行排序,但会返回一个已排序的序列
调用OrderBy
后,需要再次设置pedidos
pedidos.OrderBy(pedido => pedido.fechaEntrega);
这不会修改pedidos
集合,只会生成一个有序的可枚举项(从技术上讲是iorderenumerable
)
如果pedidos是列表
使用
pedidos.Sort((x, y) => x.fechaEntrega.CompareTo(y.fechaEntrega));
如果fechaEntrega
是可以null
的内容,则使用
var comparer = Comparer<type of fechaEntrega>.Default;
pedidos.Sort((x, y) => comparer.Compare(x.fechaEntrega, y.fechaEntrega));
var comparer=comparer.Default;
pedidos.Sort((x,y)=>comparer.Compare(x.fechaEntrega,y.fechaEntrega));
其中,粪便类型
是粪便类型
List.Sort
修改对其进行排序的基础列表。不清楚您在问什么。请澄清您的问题。您的输入和预期输出是什么?LINQ不执行OrderBy,您必须将结果分配给某些内容。请同时提及PlantBw的完整代码。顺便说一句,我是西班牙人,这是一个很好的示例,说明了为什么我讨厌许多专业人员不能理解计算机科学和说英语!!!!!!!!!Horriblent不仅如此,但它会延迟,因此如果您多次使用IEnumerable,它每次都会重新进行排序。@ScottChamberlain好吧,在一天结束时,如果您了解OrderBy返回一个有序的可枚举项………..这样做了,pedidos=(List)pedidos.OrderBy(p=>p.fechaEntrega);,工作了,非常感谢@阿古斯丁没问题!!这不起作用,我不知道为什么,但我用pedidos=(List)pedidos.OrderBy(p=>p.fechaEntrega)解决了它;非常感谢您抽出时间!我不知道为什么,但后来它坏了,我用完了这个解决方案。非常感谢,你帮了很多忙@xanatos@Augustin您不能简单地将某些东西(OrderBy的结果)转换为您想要的,并希望它能够工作。它可能会在编译器中运行,但会在运行时中断。