基于日期的两个 table 计数

Counts of two table based on dates

我有两个 tables 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

IAApplications

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();  

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

我知道我的查询有问题,这就是日期不显示分组依据和数据计数的原因

I have prefer this example.

通常,您必须有过滤器 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,这些值来自 min/max of 2 tables

可以使用到数据库的单次往返来解决该任务:

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 不支持完全外连接,但它可以模拟为左外连接和右外连接的并集,如上所示。