2011-10-16 59 views
2

我有一個WPF C#應用程序,它通過SQL-LINQ連接訪問數據並將其放在Datagrid上。我已經添加了一個Running Pips列,並一直試圖弄清楚如何正確填充此列。我通過搜索網絡發現了幾種方法,但這些方法都不適合我的特定設置。大多數時候我會陷入無限循環。我希望Running Pips列是累積的,並且計算的順序是從最早的「關閉時間」開始的。使用LINQ to SQL計算運行總數

Close Time Profit Running Profit 
10.09.11  $10   $10 
10.10.11  $20   $30 
10.11.11  $15   $45 

這裏是代碼摘要。我希望有人可以建議如何與需要幫助我實現這個任何相關信息,解決這個問題的代碼:

public void RefreshClose() { 
    if (CloseTradeCollection == null) return; 
    var i = 0; 

    if (StaticTool.SelectedAccount == null) { 
     ExistCloseTrade = false; 
     CloseTradeCollection.Clear(); 
     return; 
    } 

    Account = StaticTool.SelectedAccount; 

    foreach (var trade in GetClosedTradesFromDb()) { 
     if (CloseTradeCollection.Count <= i) { 
      ExistCloseTrade = true; 
      var tradeDetails = new Trade { 
       Id = trade.id, 
       Ticket = trade.ticket, 
       OpenTime = trade.opentime, 
       CloseTime = trade.closetime, 
       Symbol = trade.symbol, 
       OpenPrice = trade.openprice, 
       ClosePrice = trade.closeprice, 
       Profit = trade.profit, 
       Comment = trade.comment.Trim(), 
      }; 

      tradeDetails.History = tradeDetails.CloseTime - tradeDetails.OpenTime; 
      CloseTradeCollection.Add(tradeDetails); 
     } 

     else { 
      var tradeDetails = CloseTradeCollection[i]; 
      tradeDetails.Id = trade.id; 
      tradeDetails.Ticket = trade.ticket; 
      tradeDetails.OpenTime = trade.opentime; 
      tradeDetails.CloseTime = trade.closetime; 
      tradeDetails.Symbol = trade.symbol; 
      tradeDetails.OpenPrice = trade.openprice; 
      tradeDetails.ClosePrice = trade.closeprice; 
      tradeDetails.Profit = trade.profit; 
      tradeDetails.Comment = trade.comment.Trim(); 
      tradeDetails.History = DateTime.Now - tradeDetails.OpenTime; 

      //tradeDetails.RunningProfit = ???????? 

     } 

     i++; 
    } 
} 

回答

2

您可以創建一個局部變量來跟蹤運行的利潤,利用這樣的事實,即轉讓操作返回正在分配的值。

tradeDetails.RunningProfit = (runningProfit = runningProfit + trade.profit); 
+0

謝謝!你的代碼工作。我剛剛添加了以下行:double runningProfit = 0;這太容易了! – Stan

0

這是一個總的想法 - 你應該能夠將其採納你的需求:

class Trade { 
    public DateTime CloseTime { get; set; } 
    public decimal Profit { get; set; } 
} 

class TradeWithRunningProfit : Trade { 
    public decimal RunningProfit { get; set; } 
} 

class Program { 

    static IEnumerable<TradeWithRunningProfit> GetRunningProfits(IEnumerable<Trade> rows) { 

     decimal running_profit = 0; 

     return 
      from row in rows 
      select new TradeWithRunningProfit { 
       CloseTime = row.CloseTime, 
       Profit = row.Profit, 
       RunningProfit = (running_profit += row.Profit) 
      }; 

    } 

    static void Main(string[] args) { 

     var rows = new[] { 
      new Trade { CloseTime = new DateTime(11,10,09), Profit = 10}, 
      new Trade { CloseTime = new DateTime(11,10,10), Profit = 20}, 
      new Trade { CloseTime = new DateTime(11,10,11), Profit = 15}, 
     }; 

     foreach (var row_with_running_profit in GetRunningProfits(rows)) { 
      Console.WriteLine(
       "{0}\t{1}\t{2}", 
       row_with_running_profit.CloseTime, 
       row_with_running_profit.Profit, 
       row_with_running_profit.RunningProfit 
      ); 
     } 

    } 

} 
+0

感謝您的反饋。我在控制檯中運行了你的代碼並且看到它有效。但是,我不夠熟練,無法在我的代碼中對其進行修改。我希望這會更容易解決。 – Stan