List model --> orderbydescending System.DateTime does not work?

13,229

OrderByDescending method does not order in place, you need to re-assign again:

modelMyInventory = modelMyInventory.OrderByDescending(m => m.StockDate);
Share:
13,229
fletchsod
Author by

fletchsod

I'm a programmer working for software company for car dealership. I have done some .NET, JavaScript, C# and PHP. My dream is to have lot of free time working on open source software to help benefit people.

Updated on June 05, 2022

Comments

  • fletchsod
    fletchsod almost 2 years

    Sorry if this is a duplicate but I can't find a StackOverflow post that works for me.

    I'm getting miffed over learning how to use list model w/ linq. My problem here is sorting by DateTime had no effect. I'm using .NET framework v4.5. I'm using SQL DataReader to read data into list model, but instead of writing/posting sql object, I'm gonna manually specify the adding of data to list model manually for this posting.

    public MyInventory : IDisposable
    {
        public MyInventory {
            PurchaseId = -1;
            StockDate = null;
        }  
        public void Dispose() {
            //PurchaseId...
            StockDate = null;
        } 
        public long PurchaseId { get; set; }
        public DateTime? StockDate { get; set; }
    }
    
    List<MyInventory> modelMyInventory = new List<MyInventory>();
    
    modelMyInventory.Add(new MyInventory { PurchaseId = 2, StockDate = DateTime.Parse("01-02-2010") });
    modelMyInventory.Add(new MyInventory { PurchaseId = 5, StockDate = DateTime.Parse("01-03-2011") });
    modelMyInventory.Add(new MyInventory { PurchaseId = 7, StockDate = DateTime.Parse("01-01-2010") });
    
    modelMyInventory.OrderByDescending(m => m.StockDate);
    

    Thanks...