每个更改行列的LINQ和DateTime计算 [英] LINQ and DateTime Calculation for every change row column

查看:66
本文介绍了每个更改行列的LINQ和DateTime计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我遇到一个需要知道响应的问题.
我的第一个问题是,我是LINQ语言的新手,所以我无法对此进行更深入的介绍.
我的第二个问题是我有一些数据,例如以下内容(数千行和数百个csv文件):

Hi folks,
I am encountering on a problem that I need to know the response.
My first problem is that I am new on LINQ language so I cannot go deeper on that yet.
My second problem is that I have some bunch of data like following (thousands of row and hundreds of csv files) :

00:00:28	TEST1
00:00:30	TEST1
00:00:34	TEST1
00:00:31	TEST1
00:00:34	TEST1
00:02:48	TEST2
00:03:24	TEST2
00:02:48	TEST2
00:02:56	TEST3
12:58:20	TEST4
08:10:41	TEST4
07:57:38	TEST4
12:58:20	TEST4
00:01:49	TEST5
00:01:04	TEST5
00:01:49	TEST5
00:01:04	TEST5
00:01:49	TEST5


两列用制表符"分隔.
我需要的是计算:
-平均,
-最大值
第二列的每次更改.
的意思是:
-TEST1:


Two columns separated by "tab".
What I need to have is calculate the :
- average,
- maximum
of every change of the second column.
Means for :
- TEST1 :

* average: 00:00:31
* maximum: 00:00:34



-TEST2:



- TEST2 :

* average: 00:03:00
* maximum: 00:03:24



-TEST3:



- TEST3 :

* average: 00:02:56
* maximum: 00:02:56


-TEST4:


- TEST4 :

* average: 10:31:15
* maximum: 12:58:20


-TEST5:


- TEST5 :

* average: 00:01:31
* maximum: 00:01:49



就像在excel软件中每次更改列时计算小计一样,但是在我的情况下,我想将LINQ/DataTable与C#一起使用.

有谁能指出我这样做的正确方法?

到目前为止,我所做的就是:



Like calculating the subtotal at each change of a column in excel software, but in my case I want to use LINQ/DataTable with C#.

Is anyone can point me to the right way to do that ?

What I have done so far from here is this :

var qry = from row in dtTbl.AsEnumerable()
                              group row by row.Field<string>("t_Source") into grp
                              select new
                                         {
                                             sum = grp.Sum(r=>r.Field<int>("id"))
                };
                    foreach (var grp in qry)
                    {
                        Console.Write(String.Format("Sum:{0}", grp.sum));
                    }


只是为了测试,但没有输出正确的结果,我得到了错误.

预先感谢您的帮助.


Just for test but did not output the right result, I got error.

Thanks in advance for your help.

推荐答案

如果试图模拟您的代码(和DataTable).我遇到的问题是id字段(我假设它是包含时间的字段)包含datetime值而不是int.因此,为了计算总和,我将其更改为
If tried to simulate your code (and DataTable). The problem I ran into was that id field (I assume that it is the field containing the time) contains a datetime value instead off a int. So to calculate the sum, I changed it into
var qry = from row in dtTbl.AsEnumerable()
    group row by row.Field<string>("t_Source") into grp
    select new
    {
       sum = grp.Sum(r=>r.Field<datetime>("id").TimeOfDay.TotalSeconds)
    };



将代码重写为您可以使用的解决方案,如下所示:



Rewriting the code to a solution you could use, could look something like this:

var qry = from row in dtTbl.AsEnumerable()
    group row by row.Field<string>("t_Source") into grp
    select new
    {
        source = grp.Key,
        avg = new TimeSpan(0, 0, (int)(grp.Average(r => r.Field<datetime>("id").TimeOfDay.TotalSeconds))),
        max = new TimeSpan(0, 0, (int)(grp.Max(r => r.Field<datetime>("id").TimeOfDay.TotalSeconds)))
    };

foreach (var grp in qry2)
{
    Console.WriteLine(String.Format("Source {0}: average = {1}, max = {2}", grp.source, grp.avg, grp.max));
}


这篇关于每个更改行列的LINQ和DateTime计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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