如何从嵌套的 select 查询 linq 中读取数据
How to read data from a nested select query linq
我有一个像下面这样的 linq 类型的查询。
var querymiangin = (from t1 in _context.Apiapplicant
join t2 in _context.ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "granted"
select new { A = t1, B = t2, Year = t1.ApiRequestDate.Substring(0, 4), Month = t1.ApiRequestDate.Substring(5, 2) } into joined
group joined by new { joined.Year, joined.Month, joined.B.LastReqStatus } into grouped
select grouped.Select(g => new { ApiReqDate = g.A.ApiRequestDate, ApiDate = g.B.Date, ApiLastReqStatus = g.B.LastReqStatus, ApiYear = g.Year, ApiMonth = g.Month })).ToList();
在select部分,ApiReqDate和ApiDate有多个记录。现在我的问题是每个月组和年组,我有多个 ApiDate 和 ApiReqDate 记录,我希望每个组都基于条件 (t1.LastRequestStatus == t2.Id && t3.Name == "granted") 通过GetPersianDaysDiffDate()方法,获取每个月ApiReqDate与其相关ApiDate记录的差值,然后求出当月的平均值。
为此,我编写了如下代码:
var avgDateDiff = querymiangin.DefaultIfEmpty()
.GroupBy(x => new { x.ApiYear, x.ApiMonth }, (key, g) => new
{
key.ApiYear,
key.ApiYear,
Avg = g.Average(y => GetPersianDaysDiffDate(y.ApiReqDate,y.ApiDate))
})
.ToList();
但问题是每个参数 x.ApiYear、x.ApiMonth、y.ApiReqDate、y.ApiDate 都是未知的,它显示错误。如果有人可以建议我解决这个问题,我将不胜感激。
1 - 对于第一个请求 querymiangin
,您不需要 group by
语句,只需将代码更改为 :
var querymiangin = (from t1 in Apiapplicant
join t2 in ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
join t3 in EntityType on t2.LastReqStatus equals t3.Id
where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "granted"
select new
{
ApiReqDate = t1.ApiRequestDate,
ApiDate = t2.Date,
ApiYear = t1.ApiRequestDate.Substring(0, 4),
ApiMonth = t1.ApiRequestDate.Substring(5, 2)
}).ToList();
2 - 对于第二个查询 avgDateDiff
,通过 ApiYear
和 ApiMonth
使用 GroupBy
并计算 Average
,例如:
var avgDateDiff = querymiangin
.GroupBy(x => new { x.ApiYear, x.ApiMonth }, (key, g) => new
{
key.ApiYear,
key.ApiMonth,
Avg = g.Average(y => GetPersianDaysDiffDate(y.ApiReqDate, y.ApiDate))
}).ToList();
希望对您有所帮助。
我有一个像下面这样的 linq 类型的查询。
var querymiangin = (from t1 in _context.Apiapplicant
join t2 in _context.ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "granted"
select new { A = t1, B = t2, Year = t1.ApiRequestDate.Substring(0, 4), Month = t1.ApiRequestDate.Substring(5, 2) } into joined
group joined by new { joined.Year, joined.Month, joined.B.LastReqStatus } into grouped
select grouped.Select(g => new { ApiReqDate = g.A.ApiRequestDate, ApiDate = g.B.Date, ApiLastReqStatus = g.B.LastReqStatus, ApiYear = g.Year, ApiMonth = g.Month })).ToList();
在select部分,ApiReqDate和ApiDate有多个记录。现在我的问题是每个月组和年组,我有多个 ApiDate 和 ApiReqDate 记录,我希望每个组都基于条件 (t1.LastRequestStatus == t2.Id && t3.Name == "granted") 通过GetPersianDaysDiffDate()方法,获取每个月ApiReqDate与其相关ApiDate记录的差值,然后求出当月的平均值。
为此,我编写了如下代码:
var avgDateDiff = querymiangin.DefaultIfEmpty()
.GroupBy(x => new { x.ApiYear, x.ApiMonth }, (key, g) => new
{
key.ApiYear,
key.ApiYear,
Avg = g.Average(y => GetPersianDaysDiffDate(y.ApiReqDate,y.ApiDate))
})
.ToList();
但问题是每个参数 x.ApiYear、x.ApiMonth、y.ApiReqDate、y.ApiDate 都是未知的,它显示错误。如果有人可以建议我解决这个问题,我将不胜感激。
1 - 对于第一个请求 querymiangin
,您不需要 group by
语句,只需将代码更改为 :
var querymiangin = (from t1 in Apiapplicant
join t2 in ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
join t3 in EntityType on t2.LastReqStatus equals t3.Id
where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "granted"
select new
{
ApiReqDate = t1.ApiRequestDate,
ApiDate = t2.Date,
ApiYear = t1.ApiRequestDate.Substring(0, 4),
ApiMonth = t1.ApiRequestDate.Substring(5, 2)
}).ToList();
2 - 对于第二个查询 avgDateDiff
,通过 ApiYear
和 ApiMonth
使用 GroupBy
并计算 Average
,例如:
var avgDateDiff = querymiangin
.GroupBy(x => new { x.ApiYear, x.ApiMonth }, (key, g) => new
{
key.ApiYear,
key.ApiMonth,
Avg = g.Average(y => GetPersianDaysDiffDate(y.ApiReqDate, y.ApiDate))
}).ToList();
希望对您有所帮助。