基于日期的两个表的计数

时间:2018-09-10 13:58:52

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

我有两个表 Registers IAApplications 如下

注册

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

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

enter image description here

我知道查询中出了点问题,这就是为什么日期不显示分组依据以及数据计数的原因

I have prefer this example

2 个答案:

答案 0 :(得分:0)

通常,您必须具有过滤器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

答案 1 :(得分:0)

可以通过单次往返数据库来解决任务:

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