如何获得linq的剩余价值?

时间:2017-06-11 11:01:34

标签: c# linq

我有List包含:(没有保留列数据)

Recive      Pay     Remain
---------------------------------------------
2            0       0  
0            4       0  
3            0       0  
4            0       0  

我有initialRecieve=1变量

如何计算C#中的剩余列以获得以下结果?

Recive      Pay     Remain
---------------------------------------------
2            0       3  (initialRecieve+Recive-Pay)
0            4      -1  (Remain+Recive-Pay)
3            0       2  (Remain+Recive-Pay)
4            0       6  (Remain+Recive-Pay)

2 个答案:

答案 0 :(得分:2)

我根据您的需要创建一个简单的控制台应用程序

{ Receive = 2, Pay = 0, Remain = 3 }
{ Receive = 0, Pay = 4, Remain = -1 }
{ Receive = 3, Pay = 0, Remain = 2 }
{ Receive = 4, Pay = 0, Remain = 6 }

结果将是:

On Error Resume Next

ActiveSheet.Unprotect Password:="generation@34"
ActiveSheet.ShowAllData

ActiveSheet.ProtectPassword:="gerenation@34" DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingRows:=True,           AllowSorting:= _
True, AllowFiltering:=True,  AllowUsingPivotTables:=True,Userinterface:=True


 End Sub

DotNetFiddle

上查看

答案 1 :(得分:1)

只需执行以下操作:

for (var i = 0; i < DataTable.Rows.Count; i++)
{
   if (i != 0)
   {
      DataTable.Rows[i][2] = (DataTable.Rows[i - 1][2] + DataTableRows[i][0]) - DataTable.Rows[i][1];
   }
   else if (i == 0) DataTable.Rows[i][2] = (InitialRecieve + DataTable.Rows[i][0]) - DataTable.Rows[i][1]; 
}

这应该用内存中的数据来计算你需要的东西。