在 Master 中对记录进行分组,但希望对 Details 进行计数
Group records in Master but want count for Details
请考虑这些 table:
大师:
Id Year Season Flag City
------------------------------------------------------------------------------
1 2020 1 8000 Paris
2 2020 1 7000 Paris
3 2020 1 9000 London
4 2020 2 3000 Tokyo
5 2020 2 1000 Paris
6 2020 3 2000 Tokyo
7 2020 1 1000 London
8 2019 4 8000 Paris
9 2019 4 2000 Paris
详情:
Id MasterId Year Season CurrentFlag
--------------------------------------------------------------------------------------
1 8 2020 1 8000
2 9 2020 1 2500
3 8 2020 2 8100
4 1 2020 2 8000
5 2 2020 2 7500
6 2 2020 3 7500
7 3 2020 2 6000
8 4 2020 3 5000
9 7 2020 2 4000
将 Master
视为我记录生命周期的开始,将 Details
视为按季节显示 CurrentFlag
。现在我想要一个 Linq
查询,为 (Year == 2010 and Season == 1)
:
带来这个结果
City (Count Master All) (Count Master Flag<8000) (Count Details All) (Count Details Current Flag<8000)
---------------------------------------------------------------------------------------------------------
Paris 2 1 2 1
London 1 1 0 0
我写了这个查询:
var Records = (from m in master
join d in details
on m.Id equals d.MasterId into outerJoin
from d in outerJoin
where (m.Year == year && m.Dore == dore) ||
(d.Year == year && d.Dore == dore)
group m by new { m.City } into grp <--------
select new
{
City = grp.Key.City,
Count_Master_All = grp.Count(),
Count_Master_Below_8000 = grp.Where(o => o.Flag < 8000),
Count_Details_All = ???
Count_Details_Below_8000 = ???
}).ToList();
第一个问题是我只能用 m
对记录进行分组,所以我无法计算详细信息 table 的需求记录。我该怎么做?
谢谢
稍后在查询中做组
var Records = (from m in master
join d in details
on m.Id equals d.MasterId into outerJoin
from d in outerJoin
where (m.Year == year && m.Dore == dore) ||
(d.Year == year && d.Dore == dore)
select new {m = m, d = d})
.GroupBy(x => new { x.m.City })
.Select(grp => new
{
City = grp.Key.City,
Count_Master_All = grp.Count(),
Count_Master_Below_8000 = grp.Where(o => o.Flag < 8000),
Count_Details_All = grp.Count()
}).ToList();
请考虑这些 table:
大师:
Id Year Season Flag City
------------------------------------------------------------------------------
1 2020 1 8000 Paris
2 2020 1 7000 Paris
3 2020 1 9000 London
4 2020 2 3000 Tokyo
5 2020 2 1000 Paris
6 2020 3 2000 Tokyo
7 2020 1 1000 London
8 2019 4 8000 Paris
9 2019 4 2000 Paris
详情:
Id MasterId Year Season CurrentFlag
--------------------------------------------------------------------------------------
1 8 2020 1 8000
2 9 2020 1 2500
3 8 2020 2 8100
4 1 2020 2 8000
5 2 2020 2 7500
6 2 2020 3 7500
7 3 2020 2 6000
8 4 2020 3 5000
9 7 2020 2 4000
将 Master
视为我记录生命周期的开始,将 Details
视为按季节显示 CurrentFlag
。现在我想要一个 Linq
查询,为 (Year == 2010 and Season == 1)
:
City (Count Master All) (Count Master Flag<8000) (Count Details All) (Count Details Current Flag<8000)
---------------------------------------------------------------------------------------------------------
Paris 2 1 2 1
London 1 1 0 0
我写了这个查询:
var Records = (from m in master
join d in details
on m.Id equals d.MasterId into outerJoin
from d in outerJoin
where (m.Year == year && m.Dore == dore) ||
(d.Year == year && d.Dore == dore)
group m by new { m.City } into grp <--------
select new
{
City = grp.Key.City,
Count_Master_All = grp.Count(),
Count_Master_Below_8000 = grp.Where(o => o.Flag < 8000),
Count_Details_All = ???
Count_Details_Below_8000 = ???
}).ToList();
第一个问题是我只能用 m
对记录进行分组,所以我无法计算详细信息 table 的需求记录。我该怎么做?
谢谢
稍后在查询中做组
var Records = (from m in master
join d in details
on m.Id equals d.MasterId into outerJoin
from d in outerJoin
where (m.Year == year && m.Dore == dore) ||
(d.Year == year && d.Dore == dore)
select new {m = m, d = d})
.GroupBy(x => new { x.m.City })
.Select(grp => new
{
City = grp.Key.City,
Count_Master_All = grp.Count(),
Count_Master_Below_8000 = grp.Where(o => o.Flag < 8000),
Count_Details_All = grp.Count()
}).ToList();