0

我现在习惯了 linq,回到 .net 2.0 很痛苦。

我需要按日期获取最后 12 条记录。我构建了一个 noddy 示例来解释问题。我对我的编码方式不满意。我在列表中使用反向来完成这项工作。

有没有更简洁的方法?

这是我的代码:

    class Program
        {
            private static void Main(string[] args)
            {
                var allOrders = BuildOrders();

                //Reverse so that we can get the orders in date desc and easier to get last 12
                allOrders.Reverse();
                int count = 0;
                List<Order>onlyLast12Orders=new List<Order>();
                foreach (Order order in allOrders)
                {
                    count++;
                    if(count>12)break;
                    onlyLast12Orders.Add(order);
                }
                //Reverse again so that it can be displayed in date order
                onlyLast12Orders.Reverse();
                foreach (Order ord in onlyLast12Orders)
                {
                    Console.WriteLine("Order Name :{0} Date: {1}", ord.Name, ord.Date);
                }
                Console.Read();
            }

            private static List<Order> BuildOrders()
            {
                List<Order> allOrders = new List<Order>();
                for (int i = 0; i < 30; i++)
                {
                    Order order = new Order("Order" + i, new DateTime(2013, 1, 1).AddDays(i));
                    allOrders.Add(order);
                }
                return allOrders;
            }
        }
        public class Order
        {
            public Order(string name ,DateTime date)
            {
                this.name = name;
                this.date = date;
            }
            private string name;
            private DateTime date;

            public string Name
            {
                get { return name; }
                set { name = value; }
            }

            public DateTime Date
            {
                get { return date; }
                set { date = value; }
            }
        }

关于如何改进上述代码以获取最后 12 条记录的任何建议?

谢谢

4

4 回答 4

3

你有一个List<Order>- 这意味着你知道计数和GetRange方法。

var allOrders = BuildOrders();
// Take at most 12, but don't try to take more orders than actually exist!
int rangeCount = Math.Min(12, allOrders.Count);
var lastOrders = allOrders.GetRange(allOrders.Count - rangeCount, rangeCount);

您还应该考虑使用LINQBridge - .NET 2.0 的 LINQ to Objects 实现。

于 2013-09-19T06:23:52.157 回答
0

我的优化将在构建订单时进行,维护排序顺序,然后只使用一个循环来获取项目数。

于 2013-09-19T06:30:38.207 回答
0

Stack<Order>也适用于这种情况:

private static void Main(string[] args)
        {
            var allOrders = BuildOrders();

            Order tempOrder;
            for (int i = 0; i < 12; i++)
            {
                tempOrder =  allOrders.Pop();
                Console.WriteLine("Order Name :{0} Date: {1}", tempOrder.Name, tempOrder.Date);
                if (counter >= 12)
                    break;
            }
        }

        private static Stack<Order> BuildOrders()
        {
            Stack<Order> allOrders = new Stack<Order>();
            for (int i = 0; i < 30; i++)
            {
                Order order = new Order("Order" + i, new DateTime(2013, 1, 1).AddDays(i));
                allOrders.Push(order);
            }
            return allOrders;
        }

编辑:

在 Main(string[] args) 中如何:

    private static void Main(string[] args)
    {
        var allOrders = BuildOrders();
        Stack<Order> temp = new Stack<Order>();
        for (int i = allOrders.Count - 1; i >= allOrders.Count - 12; i--)
        {
            temp.Push(allOrders[i]);
        }
        foreach (var order in temp)
        {
            Console.WriteLine("Order name {0} and date: {1}", order.Name, order.Date);
        }
    }
于 2013-09-19T06:43:13.733 回答
-1

您可以在一个 for 循环中完成所有任务

List<Order> newList = BuildOrders();

List<Order> reverseList = new List<Order>();

for (int i = (newList.Count - 1);
    i > ((newList.Count - 1) - 12) && i >= 0; i--)
{
    reverseList.Add(newList[i]);
}
于 2013-09-19T06:34:08.290 回答