使用LINQ to SQL计算运行总计

时间:2011-10-16 12:53:40

标签: c# linq-to-sql

我有一个WPF C#应用程序,它通过SQL-LINQ连接访问数据并将其放在Datagrid上。我添加了一个Running Pips列,并一直试图找出如何正确填充此列。我通过搜索网络找到了几种方法,但这些方法都不适合我的特定设置。大部分时间我都是无休止的循环。我希望Running Pips列是累积的,并且计算的顺序从最早的'Close Time'开始。

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 个答案:

答案 0 :(得分:2)

您可以创建一个局部变量来跟踪运行利润,利用赋值操作返回所分配值的事实。

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

答案 1 :(得分: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
            );
        }

    }

}