我写了以下代码来计算引用Document
到:
var currentStationId = GetCurrentStation();
var result = dbContext
.Documents
.Select(x=>
new MyDTO{
...,
ReferCounts = x.DocumentStationHistories
.Count(t => t.ToStationId == currentStationId))
}
现在我想改变计算ReferCounts
:
if(currentStationId == 1) ReferCounts = x.DocumentStationHistories
.Count(t => t.ToStationId == currentStationId &&
t.FromStationId != t.ToStationId))
else
ReferCounts = x.DocumentStationHistories
.Count(t => t.ToStationId == currentStationId))
如何在我的linq实体查询中使用此代码?
答案 0 :(得分:0)
以下是ternary if语法的示例。
var result = dbContext
.Documents
.Select(x=>
new MyDTO{
...,
ReferCounts = (currentStationId == 1) ?
x.DocumentStationHistories.Count(t => t.ToStationId == currentStationId && t.FromStationId != t.ToStationId))
: x.DocumentStationHistories.Count(t => t.ToStationId == currentStationId))
}