【发布时间】:2021-05-05 19:51:06
【问题描述】:
我正在尝试计算状态统计信息,但问题是当我进行分组时,我没有得到 "null" 统计信息。
Parent:
Id Status
1 Processed
2 Hold
3 Review
4 Closed
5 Rejected
6 Draft
Child :
Id UserId ParentId
1 100 1
2 100 1
3 100 1
4 100 1
5 100 2
6 100 2
7 100 2
8 100 3
4 100 3
8 100 4
4 100 4
9 100 5
10 100 6
11 100 null
12 100 null
预期输出:
Processed = 4
Hold = 3
Review = 2
Closed = 2
Rejected = 1
Draft = 1
Null = 2
查询:
var data = (from c in context.Child
where c.UserId == 100 &&
(c.Parent ==null || c.Parent.IsActive)
group c by new
{
c.ParentId,
c.Parent.Status
} into g
select new StatusWiseStatsResult
{
Status = g.Key.Status,
Statistics = g.Count(),
Id = g.Key.ParentId == null ? 0 : (int)g.Key.ParentId
}).OrderByDescending(c => c.Statistics).ToList();
但它不会在上述查询中返回 2 的“空”计数。
谁能告诉我是什么问题和解决方法?
【问题讨论】:
-
不是你的问题,而是
where c.UserId == 100 && c.UserId == 100,你有这个条件两次;也许是 o 型? -
@Codexer 抱歉,这是一个错字。感谢您指出。更新了问题:)
-
这是哪个数据库提供商?我尝试使用 EF6 + Sql Server 进行类似查询,但无法重现。
-
@GertArnold 我正在使用 Sql Server
-
这是来自您的代码库的确切查询吗?其他谓词可以很容易地将外连接变成事实上的内连接。
标签: c# entity-framework linq entity-framework-6