创建按月汇总的数据表

时间:2018-10-10 18:19:42

标签: c# linq

我有2个数据库表,连接到下面的FK(ResponseHeader)示例中(示例中的日期是10月6日)

ResponseHeader

ResponseHeaderId                        DateTime
0e24cf96-81eb-2122-7e4a-0d200474692f    06/10/2018 11:15:59

ResponseData

ResponseDataId                          ResponseHeaderId                        Response
41c831f1-0adc-2bd5-053e-00406fa526b6    0e24cf96-81eb-2122-7e4a-0d200474692f    1   
78967068-82a6-4098-ba35-03211a923f46    0e24cf96-81eb-2122-7e4a-0d200474692f    2
854bc8a6-5877-a6fb-9072-00e358323350    0e24cf96-81eb-2122-7e4a-0d200474692f    2   
fe2a667d-ca0e-49a6-b330-f4d4232bfe89    0e24cf96-81eb-2122-7e4a-0d200474692f    3
30f0270e-3e69-3408-7add-02a85f4b9aeb    0e24cf96-81eb-2122-7e4a-0d200474692f    1

有多个ResponseHeader,每个响应头都有5行数据)

每组响应数据的响应都需要加起来(示例= 9)

我想要实现的是一张像这样的表,其中所有数据都按给定年份的月总计。

2018
Month        Responses        Sum
October      1                9

但是,由于每个Responseheader都有多个行,所以我最终想要的是:

2018
Month        Responses        Sum
October      185              234
September    564              985
...
2017
Month        Responses        Sum
January      54               123
...

我有linq可以按月求和,例如:

var total = 0;
var filteredResponses = Model.SurveyResponseHeader.Where(x => x.StartDate > DateTime.Today.LastMonth() && x.StartDate > DateTime.Today.AddMonths(1).LastMonth()).ToList();

foreach (var response in filteredResponses)
{
    var responseTotal = (response.SurveyResponses.Sum(x => x.Response));
    total += responseTotal;
}

@total

鉴于所有这些,我该怎么办?

  • 按月浏览数据,汇总该月的每个响应数据集
  • 按月和年对数据分组

我对使用基本的linq和C#进行按日期查询和求和的数据没有问题,但是像这样进行分组和求和的工作超出了我

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            DataTable dt1 = new DataTable();

            dt1.Columns.Add("ResponseHeaderId", typeof (string));
            dt1.Columns.Add("DateTime", typeof (DateTime));

            dt1.Rows.Add(new object[] { "0e24cf96-81eb-2122-7e4a-0d200474692f", DateTime.Parse("06/10/2018 11:15:59") });
            dt1.Rows.Add(new object[] { "0e24cf96-81eb-2122-7e4a-0d2004746930", DateTime.Parse("05/10/2018 11:15:59") });
            dt1.Rows.Add(new object[] { "0e24cf96-81eb-2122-7e4a-0d2004746931", DateTime.Parse("04/10/2018 11:15:59") });
            dt1.Rows.Add(new object[] { "0e24cf96-81eb-2122-7e4a-0d2004746932", DateTime.Parse("03/10/2017 11:15:59") });
            dt1.Rows.Add(new object[] { "0e24cf96-81eb-2122-7e4a-0d2004746933", DateTime.Parse("02/10/2017 11:15:59") });

            DataTable dt2 = new DataTable();

            dt2.Columns.Add("ResponseDataId", typeof (string));
            dt2.Columns.Add("ResponseHeaderId", typeof (string));
            dt2.Columns.Add("Response", typeof (int));


            dt2.Rows.Add(new object[] { "41c831f1-0adc-2bd5-053e-00406fa526b6","0e24cf96-81eb-2122-7e4a-0d200474692f", 1 });  
            dt2.Rows.Add(new object[] { "78967068-82a6-4098-ba35-03211a923f46","0e24cf96-81eb-2122-7e4a-0d200474692f", 2 });
            dt2.Rows.Add(new object[] { "854bc8a6-5877-a6fb-9072-00e358323350","0e24cf96-81eb-2122-7e4a-0d2004746930", 2 });
            dt2.Rows.Add(new object[] { "fe2a667d-ca0e-49a6-b330-f4d4232bfe89","0e24cf96-81eb-2122-7e4a-0d2004746931", 3 });
            dt2.Rows.Add(new object[] { "30f0270e-3e69-3408-7add-02a85f4b9aeb","0e24cf96-81eb-2122-7e4a-0d2004746932", 1 });
            dt2.Rows.Add(new object[] { "30f0270e-3e69-3408-7add-02a85f4b9aeb","0e24cf96-81eb-2122-7e4a-0d2004746933", 1 });


            var join = (from d1 in dt1.AsEnumerable()
                        join d2 in dt2.AsEnumerable() on d1.Field<string>("ResponseHeaderId") equals d2.Field<string>("ResponseHeaderId")
                        select new { d1 = d1, d2 = d2 })
                        .OrderByDescending(x => x.d1.Field<DateTime>("DateTime"))
                       .GroupBy(x => new { year = x.d1.Field<DateTime>("DateTime").Year, month = x.d1.Field<DateTime>("DateTime").ToString("MMMM") })
                       .Select(x => new { year = x.Key.year, month = x.Key.month, total = x.Select(y => y.d2.Field<int>("Response")).Sum()})
                       .ToList();
        }
    }



}