[英]C# entity framework LINQ - create report
我需要在程序中实现一个功能,该功能将使用实体框架提供有关警察的统计信息。 图为数据库的结构:
我的问题是我无法正确组织多个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...)
另外,有7个表要合并并计算数量。 在所有表中,外键都是PersonId。 需要包装ReportContainer类型的对象列表的所有结果。 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
}
我会得到第一次约会,所以我可以减去7天和30天
var firstdate = Added.Where(a => a.Id == 1).Select(d => d.Date);
然后加入用户,已添加,状态和照片
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.