简体   繁体   English

C#实体框架LINQ-创建报告

[英]C# entity framework LINQ - create report

I need to implement in the program a function that will provide statistics on police using Entity framework. 我需要在程序中实现一个功能,该功能将使用实体框架提供有关警察的统计信息。 The picture shows the structure of the database: 图为数据库的结构: 在此处输入图片说明

My problem is that I can't properly organize multiple Join: 我的问题是我无法正确组织多个Join:

Users.Where(w => w.Region == 1321)
     .Select(s => s.Id)
     .Join(Added, x => x, y => y.UserId, (x, y) => new
     {
         UserId = x,
         PersonId = y.PersonId,
         Date = y.AddDate
     })
     .GroupJoin(Status, x => x.PersonId, y => y.PersonId, (x, y) => new
     {
        PersonPreviouslyConvicted = y.PersonPreviouslyConvicted,
        PersonBum = y.PersonBum,
        PersonJobless = y.Jobless,

     }).Join(Photos...).Join(More tables...)

Also, there are 7 tables that you want to merge and count the number. 另外,有7个表要合并并计算数量。 In all tables the foreign key is PersonId. 在所有表中,外键都是PersonId。 All the result of a need to wrap a list of objects of type ReportContainer. 需要包装ReportContainer类型的对象列表的所有结果。 ReportContainer as follows: ReportContainer如下:

public class ReportContainer
{
     public string Name { get; set; } // City or District name
     public int? SevenDays { get; set; } // Count of the 7 days by column Added.AddDate
     public int? ThirtyDays { get; set; } // Count of the 30 days by column Added.AddDate
     public int? Bum { get; set; } //Count Bum
     public int? Photography { get; set; } //Count Photos
     public int? PreviouslyConvicted { get; set; } //Count PreviouslyConvicted
     public int? Jobless { get; set; } //Count  Jobless

}

I would get the firstdate so I can substract 7 days and 30 days 我会得到第一次约会,所以我可以减去7天和30天

var firstdate = Added.Where(a => a.Id == 1).Select(d => d.Date);

and then Join User, Added, Status and Photos 然后加入用户,已添加,状态和照片

Users.Where(w => w.Region == 1321)
     .Select(s => s.Id)
     .Join(Added, a => a, s => s.UserId , (a, s)=> new
     {
         UserId = a.UserId,
         PersonId = s.PersonId,
         SevenDays = s.AddDate >= firstdate.AddDays(-7),
         ThirtyDays = s.AddDate >= firstdate.AddDays(-30),
     })
     .Join(Status, s => s.PersonId, y => y.PersonId, (s, y)=> new
     {
         PersonId = s.PersonId,
         Bum = y.Bum,
         Jobless = y.Jobless,
         PreviouslyConvicted = y.PreviouslyConvicted
     })
     .Join(Photos, p => p.PersonId, y => y.PersonId, (p, y) => new
     {
         PersonId = p.PersonId,
         WithPhotos= y.Photo
     })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM