c# - Comparing and allocating elements in Linq -
c# - Comparing and allocating elements in Linq -
lets have sorted (by date not shown here) list of numbers:
5, 7, 10, 4, 3, 7, 4
i want average of increases, 5 7 , 7 10 , 3 7 (2+3+5)/3 want average of decreases 10 4, 4 3 , 7 4 (6+1+3)/3
is there simple way in linq?
i calculate changes between 2 consecutive numbers first. can done enumerable.zip
of same list skipping first item. can changes moving or changes moving downwards , calculate average:
var changes = list.zip(list.skip(1), (x,y) => y-x); // [ 2, 3, -6, -1, 4, -3 ] var averageup = changes.where(x => x > 0).average(); // 3 var averagedown = changes.where(x => x < 0).average(); // -3.33
c# linq
Comments
Post a Comment