【问题标题】:Counts of two table based on dates基于日期的两个表的计数
【发布时间】:2019-02-14 23:41:50
【问题描述】:

我有两个表 RegistersIAApplications 如下

寄存器

Name                                   Email                           Creationdate
Mazhar                                Khan@gmail.com              2018-09-02 13:08:32.303
mohan                                 m@gmail.com                 2018-09-01 13:08:32.303
kjdj                                  k@gmail.com                 2018-09-01 13:08:32.303

IA应用程序

Title                Sector                   SubmissionDate
 kk                     jj                  2018-09-03 13:08:32.303 
 kk                     jj                  2018-09-02 13:08:32.303 
 mm                     tt                  2018-09-01 13:08:32.303

我的报告需要以下输出

Date            New Registratio          New Application   
09-03-2018         0                         1
09-02-2018         1                         1             
09-01-2018         2                         1   

我试过下面的代码得到错误的数据

  var Regs = from o in db.Registers
                   select new { A = (o.Creationdate) };
        var Apps = from c in db.IAApplications
                   select new { A = (c.SubmissionDate) };


        var result = (from x in Regs.Concat(Apps)
                      group x by x.A into og
                      select new { Date = og.Key, Reg = og.Count(), App = og.Count() }).ToList();  

我在本地系统中得到错误的结果,如下图所示,我有很多数据,但未提及上表问题。

我知道我的查询中有一些错误,这就是为什么日期没有显示分组依据和数据计数

I have prefer this example.

【问题讨论】:

    标签: c# asp.net linq asp.net-mvc-5


    【解决方案1】:

    通常,您必须拥有过滤器dateStartdateEnd

    然后,对于该范围内的每个日期,检查在给定日期内是否有注册或申请

    var dateStart = new DateTime(2018,9,1);
    var dateEnd = DateTime.Now;
    var list = new List<Tuple<DateTime, int, int>>(); // structure to store DateTime, New Registratio, New Application   
    for(var i = dateStart; i.Date <= dateEnd.Date;  i = i.AddDay(1))
    {
        list.Add(Tuple.Create(i,
                              db.Registers.Count(r => r.Creationdate.Date == i.Date), 
                              db.IAApplications.Count(r => r.SubmissionDate.Date == i.Date)));
    }
    var result = list.Select(og => new { Date = og.Item1, Reg = og.Item2, App = og.Item3 }).ToList();
    

    如果您没有预定义 dateStartdateEnd,这些值来自 2 个表中的 min/max

    【讨论】:

    • 我在“Admonth(1) 和第四行”之类的第二个错误中出现此错误:“System.Datetime”不包含“AddMonth”的定义并且没有扩展方法“AddMonth”接受作为 'System.DateTime' 类型的第一个参数。 @蒂埃里 V
    • @mazhar AddMonths!!
    • 我必须通过静态开始日期和结束日期日期 na,我的开始日期是 2018 年 9 月 1 日,结束日期是到现在,你可以在上面的答案中更新这个。蒂埃里 V
    • @mazhar dateStart 和 dateEnd 只是用于演示目的的变量。它们包含任何价值。循环中的重要部分。
    • 我对 linq 很陌生,请更新完整的答案我正在搜索 2 天以来,如果您更新完整的答案,这个查询对我来说非常重要。 @蒂埃里 V
    【解决方案2】:

    该任务可以通过单次往返 DB 来解决:

    var regCounts = db.Registers.GroupBy(reg => DbFunctions.TruncateTime(reg.Creationdate),
        (key, group) => new { Date = key, Value = group.Count() });
    
    var appCounts = db.IAApplications.GroupBy(app => DbFunctions.TruncateTime(app.SubmissionDate),
        (key, group) => new { Date = key, Value = group.Count() });
    
    var leftJoin = regCounts.SelectMany(reg => appCounts.Where(app => app.Date == reg.Date).DefaultIfEmpty(),
        (reg, app) => new { reg.Date, RegCount = reg.Value, AppCount = app != null ? app.Value : 0 });
    
    var rightJoin = appCounts.SelectMany(app => regCounts.Where(reg => reg.Date == app.Date).DefaultIfEmpty(),
        (app, reg) => new { app.Date, RegCount = reg != null ? reg.Value : 0, AppCount = app.Value });
    
    var data = leftJoin.Union(rightJoin) // "full join" regCounts and appCounts
        .OrderByDescending(r => r.Date)
        .ToArray();
    

    不幸的是,LINQ To Entities 不支持完全外连接,但可以将其模拟为左外连接和右外连接的联合,如上所示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      相关资源
      最近更新 更多