linq - 查找嵌套 linq 查询平均值的问题
问题描述
我正在实施 asp.net 核心项目。我有多个名为ApiApp、ApiAppHistory和Entity的表。
在ApiApp表中,我有类型为(yyyy/mm/dd) 的日期字段和一个字段。
string
ApplicantID
在ApiAppHistory中有一个名为LastReqStatus的字段,它的类型
int
和相关值存储在 Entity 表中。现在我想按
Year
、和 分组Month
,LastReqStatus
并ApplicantID
找出一年中每个月有多少等于Granted。ApplicantID
LastReqStatus
找到之后,我想知道一年中每个月的平均值。
ApplicantIDs
lastReqStatus == "Granted"
对于前者,我编写了如下查询:
var avg = from t1 in _context.Apiapp
join t2 in _context.ApiAppHistory on t1.Id equals t2.ApiAppId
join t3 in _context.Entity on t2.LastReqStatus equals t3.Id
where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "Granted"
group new {t1,t2}
by new { t2.LastReqStatus, Year = t1.ApiRequestDate.Substring(0, 4), Month = t1.ApiRequestDate.Substring(5, 2) , Applicant = t1.ApplicantId } into g
select new
{
Year = g.Key.Year,
Month = g.Key.Month,
Applicant = g.Key.Applicant,
GrantedCount = g.Count()
};
var GrantedReqAVG = avg.ToList();
public partial class ApiApplicantHistory
{
public int Id { get; set; }
public int? SentType { get; set; }
public int? Reason { get; set; }
public int? LastReqStatus { get; set; }
public int? ApiAppId { get; set; }
public virtual Apiapp ApiApp { get; set; }
public virtual EntityType LastReqStatusNavigation { get; set; }
public virtual EntityType SentTypeNavigation { get; set; }
public virtual EntityType ReasonNavigation { get; set; }
}
public partial class EntityType
{
public EntityType()
{
ApiApplicantHistoryLastReqStatusNavigation = new HashSet<ApiApplicantHistory>(); ApiApplicantHistorySentTypeNavigation = new HashSet<ApiApplicantHistory>();
ApiApplicantHistoryReasonNavigation = new HashSet<ApiApplicantHistory>();
}
public int Id { get; set; }
public string Name { get; set; }
public string EntityKey { get; set; }
public virtual ICollection<ApiApplicantHistory> ApiApplicantHistoryLastReqStatusNavigation { get; set; } public virtual ICollection<ApiApplicantHistory> ApiAppHistorySentTypeNavigation { get; set; }
public virtual ICollection<ApiApplicantHistory> ApiAppHistoryReasonNavigation { get; set; }
}
}
public partial class Apiapp
{
public Apiapp()
{
ApiApplicantHistory = new HashSet<ApiApplicantHistory>();
}
public int Id { get; set; }
public bool? IsDeleted { get; set; }
public int? LastRequestStatus { get; set; }
public int ApplicantID { get; set; }
public string ApiRequestDate { get; set; }
public virtual ICollection<ApiApplicantHistory> ApiApplicantHistory { get; set; }
}
但是关于发现平均值,我没有任何意见我该怎么做。如果有人帮助我,我将不胜感激。例如,我在ApiApp表
中有以下数据:
ApiApp:
ID:1 LastRequestStatus:1 ApplicantID:1 ApiRequestDate:2019/02/03
ID:2 LastRequestStatus:2 ApplicantID:1 ApiRequestDate:2019/02/16
ID:3 LastRequestStatus:10 ApplicantID:2 ApiRequestDate:2019/02/01
ID:4 LastRequestStatus:19 ApplicantID:2 ApiRequestDate:2019/02/20
ID:5 LastRequestStatus:31 ApplicantID:2 ApiRequestDate:2019/02/29
因此平均值应该是:
(2 + 3)/2 这意味着 2 用于 ID:1 和 ID:2,其具有相同的申请人 #1,他们在 2 月请求,3 用于 ID:3 和 ID:4 和 ID:5,其具有相同的申请人 # 2 人在 2 月提出申请,因为 2 月只有两个申请人 #1 和 #2,因此我们必须将分子除以 2。
解决方案
更新后:
1 - 您不需要分组,LastRequestStatus
只需和 Year
Month
ApplicantId
var groupedApplicant = from t1 in _context.Apiapp
join t2 in _context.ApiAppHistory on t1.Id equals t2.ApiAppId
join t3 in _context.Entity on t2.LastReqStatus equals t3.Id
where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "Granted"
group new { t1, t2 }
by new { Year = t1.ApiRequestDate.Substring(0, 4), Month = t1.ApiRequestDate.Substring(5, 2), t1.ApplicantId } into g
select new
{
Year = g.Key.Year,
Month = g.Key.Month,
Applicant = g.Key.ApplicantId
GrantedCount = g.Count()
};
该请求将给出:
Year:2019,Month:2,Applicant:1,GrantedCount:2
Year:2019,Month:2,Applicant:2,GrantedCount:3
groupedApplicant
2 -按Year
和分组Month
并选择平均值:
var avg = groupedApplicant
.GroupBy(x => new { x.Year, x.Month })
.Select(x => new { x.Key.Year, x.Key.Month, Avg = x.Average(y => y.GrantedCount) })
.ToList();
该请求将给出:
Year:2019,Month:2,Avg:2.5
我希望你觉得这有帮助。
推荐阅读
- javascript - 如何从类实例中获取 JavaScript 类构造函数参数
- node.js - UnhandledPromiseRejectionWarning:TypeError 无法读取未定义的属性“名称”
- vesta - Cannot setup VestaCP in fresh CentOS8. Failed to synchronize cache for repo 'vesta'
- google-apps-script - 如何在谷歌工作表中创建函数睡眠(毫秒)以延迟或暂停某个范围内的公式
- django - Django unknown column error when trying to get a model objects
- java - 如果成员变量关闭,如何在不将类型从 void 更改为 boolean 的情况下显示 true?
- java - 与他人共享图像和文本应用程序
- python - 避免在子命令中转义美元符号?
- spring-boot - 在 Kotlin 中使用带有嵌套属性的 @ConfigurationProperties
- github - GitHub:使用机器人读取 PR 并对该内容进行操作