Question

I am used to linq now and going back to .net 2.0 is a pain.

I need to get the last 12 records by date.I build a noddy sample to explain the problem.I am not happy with the way I coded it. I used reverse on a list to do the job.

Is there a neater way?

here is my code:

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

any suggestions on how to improve the above code to get the last 12 records?

Thanks

Was it helpful?

Solution

You've got a List<Order> - that means you know the count, and the GetRange method.

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

You should also consider using LINQBridge - a LINQ to Objects implementation for .NET 2.0.

OTHER TIPS

my optimization will be around while Building The orders , maintain the sort order and then just use one loop to get the number of items.

Stack<Order> is also suitable for such situation:

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

EDIT:

How about this in 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);
        }
    }

You can do all task in one for loop

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]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top