Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stock data carry over to next day in LINQ

Tags:

c#

linq

Last day closing stock should be the opening stock value of the next day.

I have tried this

var k = INV_STOCKs.Select(x =>
    new DemoItemV1
    {
        AreaId = x.STOCK_DATE,
        CategoryTitle = x.STOCK_QTY
    })
    .AsEnumerable()
    .Select((x, i) => { x.ID = i + 1; return x })
    .ToList();

Table structure

ID,STOCK_DATE,STOCK_QTY

Please any one can help to solve this

I need to print like this

  Date         Opening Stock      Closing Stock
01/01/13                  0                  5
01/02/13                  5                 10
01/03/13                 10                 15
01/04/13                 15                 22
01/05/13                 22                 30

Thanks in advance.

like image 564
rosa mandez Avatar asked Jul 26 '13 12:07

rosa mandez


1 Answers

"I need to print Date,opening stock,closing stock in it"

INV_STOCKs.GroupBy(x=>x.STOCK_DATE).ForEach(group=>
{
   var g = group.OrderBy(x.ID);
   Print(g.First().STOCK_DATE); //Date
   Print(g.First().STOCK_QTY); //Opening stock
   Print(g.Last().STOCK_QTY); //Closing stock
});

Print is some method printing you this value, of course you can use one method with 3 paramameters or whatever :)

EDIT: to store it in list:

class StockStore
{
   public int OpeningStock;
   public int ClosingStock;
   public DateTime Date;
}

var list = new List<StockStore>();

INV_STOCKs.GroupBy(x=>x.STOCK_DATE).ForEach(group=>
{
   var g = group.OrderBy(x.ID);
   list.Add(new StockStore 
   {
       OpeningStock = g.First().STOCK_QTY,
       ClosingStock = g.Last().STOCK_QTY,
       Date = g.First().STOCK_DATE
   }); 
});
like image 164
Kamil Budziewski Avatar answered Nov 05 '22 11:11

Kamil Budziewski