LINQ to SQL和订购的结果总运行 [英] LINQ to SQL and a running total on ordered results

查看:111
本文介绍了LINQ to SQL和订购的结果总运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 DataGridView的来显示客户的会计历史,我想有一个显示其资产负债运行总计列。旧的方式,我这样做是通过获取数据,通过数据的循环,并添加行到 DataGridView的一个接一个,计算当时的运行总计。瘸。我宁愿使用LINQ到SQL,或LINQ如果无法与LINQ到SQL,找出运行总计这样我就可以只设置 DataGridView.DataSource 我的数据。

这就是我拍摄的一个超级简单的例子。说我有下面的类。

 类项目
{
    公众的DateTime日期{搞定;组; }
    公共小数金额{搞定;组; }
    公共小数runningTotal在{搞定;组; }
}

我想一个L2S,或LINQ,报表可能产生的结果是这样的:

 发行总额runningTotal在
12-01-2009 5
12-02-2009 -5 0
12-02-2009 10 10
2009年12月3日5月15日
2009年12月4日-15 0

注意,可以有多个项目具有相同的日期(12-02-2009)。结果应以日期计算的运行总计之前进行排序。我猜这意味着我需要两种说法,一是来获取数据和排序,并第二次执行运行总计计算。

我希望总结会做的伎俩,但它不喜欢工作,我希望。或者,也许我只是无法弄清楚。

似乎这问题,我想同样的事情后去,但我没有看到接受/只回答如何解决我的问题。

这是怎么拉这一关任何想法?

修改
梳理从亚历克斯和DOK的答案,这是我结束了:

 小数runningTotal在= 0;
VAR的结果= FetchDataFromDatabase()
    .OrderBy(项目=> item.Date)
    。选择(项目=>新建项目
    {
        金额= item.Amount,
        日期= item.Date,
        runningTotal在= runningTotal在+ = item.Amount
    });


使用闭包和匿名的方式:

 列表<项目> myList中= FetchDataFromDatabase();小数currentTotal = 0;
VAR的查询= myList中
               .OrderBy(I => i.Date)
               。选择(I =>
                           {
                             currentTotal + = i.Amount;
                             返回新{
                                            日期= i.Date,
                                            金额= i.Amount,
                                            runningTotal在= currentTotal
                                        };
                           }
                      );
的foreach(查询VAR项)
{
    //做项目
}

I want to display a customer's accounting history in a DataGridView and I want to have a column that displays the running total for their balance. The old way I did this was by getting the data, looping through the data, and adding rows to the DataGridView one-by-one and calculating the running total at that time. Lame. I would much rather use LINQ to SQL, or LINQ if not possible with LINQ to SQL, to figure out the running totals so I can just set DataGridView.DataSource to my data.

This is a super-simplified example of what I'm shooting for. Say I have the following class.

class Item
{
    public DateTime Date { get; set; }
    public decimal Amount { get; set; }
    public decimal RunningTotal { get; set; }
}

I would like a L2S, or LINQ, statement that could generate results that look like this:

   Date       Amount  RunningTotal
12-01-2009      5          5
12-02-2009     -5          0
12-02-2009     10         10
12-03-2009      5         15
12-04-2009    -15          0

Notice that there can be multiple items with the same date (12-02-2009). The results should be sorted by date before the running totals are calculated. I'm guessing this means I'll need two statements, one to get the data and sort it and a second to perform the running total calculation.

I was hoping Aggregate would do the trick, but it doesn't work like I was hoping. Or maybe I just couldn't figure it out.

This question seemed to be going after the same thing I wanted, but I don't see how the accepted/only answer solves my problem.

Any ideas on how to pull this off?

Edit Combing the answers from Alex and DOK, this is what I ended up with:

decimal runningTotal = 0;
var results = FetchDataFromDatabase()
    .OrderBy(item => item.Date)
    .Select(item => new Item
    {
        Amount = item.Amount,
        Date = item.Date,
        RunningTotal = runningTotal += item.Amount
    });

解决方案

Using closures and anonymous method:

List<Item> myList = FetchDataFromDatabase();

decimal currentTotal = 0;
var query = myList
               .OrderBy(i => i.Date)
               .Select(i => 
                           {
                             currentTotal += i.Amount;
                             return new { 
                                            Date = i.Date, 
                                            Amount = i.Amount, 
                                            RunningTotal = currentTotal 
                                        };
                           }
                      );
foreach (var item in query)
{
    //do with item
}

这篇关于LINQ to SQL和订购的结果总运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆