Calculate difference from previous item with LINQ

后端 未结 7 552
你的背包
你的背包 2020-11-27 14:50

I\'m trying to prepare data for a graph using LINQ.

The problem that i cant solve is how to calculate the \"difference to previous.

the result I expect is <

7条回答
  •  被撕碎了的回忆
    2020-11-27 15:19

    Use index to get previous object:

       var LinqList = list.Select( 
           (myObject, index) => 
              new { 
                ID = myObject.ID, 
                Date = myObject.Date, 
                Value = myObject.Value, 
                DiffToPrev = (index > 0 ? myObject.Value - list[index - 1].Value : 0)
              }
       );
    

提交回复
热议问题