public List <Order> QueryOrder(OrderQueryArgs args) { IEnumerable <Order> result; switch (args.sortKey) { case SortKey.IDAscending: result = from order in orders where args.Check(order) orderby order.OrderId ascending select order; break; case SortKey.PriceAscending: result = from order in orders where args.Check(order) orderby order.Balance ascending select order; break; case SortKey.CustomerNameAscending: result = from order in orders where args.Check(order) orderby order.Customer ascending select order; break; case SortKey.IDDescending: result = from order in orders where args.Check(order) orderby order.OrderId descending select order; break; case SortKey.PriceDescending: result = from order in orders where args.Check(order) orderby order.Balance descending select order; break; case SortKey.CustomerNameDescending: result = from order in orders where args.Check(order) orderby order.Customer descending select order; break; default: result = null; break; } List <Order> resultList = new List <Order>(); foreach (Order i in result) { resultList.Add(new Order(i)); } return(resultList); }
public static Order SelectOrder(OrderService os) { Console.WriteLine("请输入订单ID,-1放弃查询,留空则使用条件查询"); int?id; do { id = ReadInt(-1, 2147483647, true); if (id.HasValue) { if (id == -1) { return(null); } Order order = os.GetOrderByID(id.Value); if (order == null) { Console.WriteLine("该订单不存在,请重新输入ID:"); continue; } else { return(order); } } } while (id.HasValue); OrderQueryArgs args = new OrderQueryArgs(); Console.WriteLine("请输入订单客户名,留空则不使用此条件"); args.customerName = Console.ReadLine(); Console.WriteLine("请输入包含的商品名,留空结束输入"); string itemName; do { itemName = Console.ReadLine(); if (itemName != "") { args.itemNames.Add(itemName); } }while (itemName != ""); Console.WriteLine("请输入最小金额,留空则不使用此条件"); args.minPrice = ReadDecimal(true) ?? -1; Console.WriteLine("请输入最大金额,留空则不使用此条件"); args.maxPrice = ReadDecimal(true) ?? -1; args.sortKey = SelectSortKey(); List <Order> orders = os.QueryOrder(args); if (orders.Count == 0) { Console.WriteLine("没有符合条件的订单"); Console.WriteLine("按任意键继续..."); Console.ReadKey(true); return(null); } for (int i = 0; i < orders.Count; i++) { Console.WriteLine($"{i + 1}. {orders[i].ToString()}"); } Console.WriteLine("请输入所选择订单的编号"); int index = ReadInt(1, orders.Count); return(orders[index - 1]); }