LET子句太多导致查询速度慢,但无法使用扩展方法

时间:2014-10-27 14:40:32

标签: c# linq entity-framework

我的实体定期读数很多(18)。这些值可能会或可能不会设置警报,因此具有“#”状态'。

我需要在linq到实体查询期间进行一些计算。我需要比较18个不同值的警报状态并获得该批次的最差状态。我无法使用读数保存状态,并在实体警报更改时重新计算。我在以有效的方式实现这个问题时遇到了问题。

这不起作用:我无法使用扩展方法... GetOverallStatus(..) cannot be translated into a store expression...

from reading in db.Readings
let OverallStatus = reading.GetOverallStatus()
select new StatusModel() { Reading = reading, Status = (StatusValue)overallStatus }).ToList();

这确实有效,但(可以理解)在调用时需要编译一个年龄:

from reading in db.Readings
let entity = reading.entity
let Value1Status = !runSpeedOK || reading.Value1 == null ? (noReadings : (!(entity.Alarm1AlarmsOn ?? false) ? (notConfig : (reading.Value1 >= entity.Alarm1Critical ? (critical : (reading.Value1 >= entity.Alarm1Warning ? (warning : (ok)))
let Value2Status = !runSpeedOK || reading.Value2 == null ? (noReadings : (!(entity.Alarm2AlarmsOn ?? false) ? (notConfig : (reading.Value2 >= entity.Alarm2Critical ? (critical : (reading.Value2 >= entity.Alarm2Warning ? (warning : (ok)))
let Value3Status = !runSpeedOK || reading.Value3 == null ? (noReadings : (!(entity.Alarm3AlarmsOn ?? false) ? (notConfig : (reading.Value3 >= entity.Alarm3Critical ? (critical : (reading.Value3 >= entity.Alarm3Warning ? (warning : (ok)))
let Value4Status = !runSpeedOK || reading.Value4 == null ? (noReadings : (!(entity.Alarm4AlarmsOn ?? false) ? (notConfig : (reading.Value4 >= entity.Alarm4Critical ? (critical : (reading.Value4 >= entity.Alarm4Warning ? (warning : (ok)))
let Value5Status = !runSpeedOK || reading.Value5 == null ? (noReadings : (!(entity.Alarm5AlarmsOn ?? false) ? (notConfig : (reading.Value5 >= entity.Alarm5Critical ? (critical : (reading.Value5 >= entity.Alarm5Warning ? (warning : (ok)))
let Value6Status = !runSpeedOK || reading.Value6 == null ? (noReadings : (!(entity.Alarm6AlarmsOn ?? false) ? (notConfig : (reading.Value6 >= entity.Alarm6Critical ? (critical : (reading.Value6 >= entity.Alarm6Warning ? (warning : (ok)))
let Value7Status = !runSpeedOK || reading.Value7 == null ? (noReadings : (!(entity.Alarm7AlarmsOn ?? false) ? (notConfig : (reading.Value7 >= entity.Alarm7Critical ? (critical : (reading.Value7 >= entity.Alarm7Warning ? (warning : (ok)))
let Value8Status = !runSpeedOK || reading.Value8 == null ? (noReadings : (!(entity.Alarm8AlarmsOn ?? false) ? (notConfig : (reading.Value8 >= entity.Alarm8Critical ? (critical : (reading.Value8 >= entity.Alarm8Warning ? (warning : (ok)))
let Value9Status = !runSpeedOK || reading.Value9 == null ? (noReadings : (!(entity.Alarm9AlarmsOn ?? false) ? (notConfig : (reading.Value9 >= entity.Alarm9Critical ? (critical : (reading.Value9 >= entity.Alarm9Warning ? (warning : (ok)))
let Value10Status = !runSpeedOK || reading.Value10 == null ? (noReadings : (!(entity.Alarm10AlarmsOn ?? false) ? (notConfig : (reading.Value10 >= entity.Alarm10Critical ? (critical : (reading.Value10 >= entity.Alarm10Warning ? (warning : (ok)))
let Value11Status = !runSpeedOK || reading.Value11 == null ? (noReadings : (!(entity.Alarm11AlarmsOn ?? false) ? (notConfig : (reading.Value11 >= entity.Alarm11Critical ? (critical : (reading.Value11 >= entity.Alarm11Warning ? (warning : (ok)))
let Value12Status = !runSpeedOK || reading.Value12 == null ? (noReadings : (!(entity.Alarm12AlarmsOn ?? false) ? (notConfig : (reading.Value12 >= entity.Alarm12Critical ? (critical : (reading.Value12 >= entity.Alarm12Warning ? (warning : (ok)))
let Value13Status = !runSpeedOK || reading.Value13 == null ? (noReadings : (!(entity.Alarm13AlarmsOn ?? false) ? (notConfig : (reading.Value13 >= entity.Alarm13Critical ? (critical : (reading.Value13 >= entity.Alarm13Warning ? (warning : (ok)))
let Value14Status = !runSpeedOK || reading.Value14 == null ? (noReadings : (!(entity.Alarm14AlarmsOn ?? false) ? (notConfig : (reading.Value14 >= entity.Alarm14Critical ? (critical : (reading.Value14 >= entity.Alarm14Warning ? (warning : (ok)))
let Value15Status = !runSpeedOK || reading.Value15 == null ? (noReadings : (!(entity.Alarm15AlarmsOn ?? false) ? (notConfig : (reading.Value15 >= entity.Alarm15Critical ? (critical : (reading.Value15 >= entity.Alarm15Warning ? (warning : (ok)))
let Value16Status = !runSpeedOK || reading.Value16 == null ? (noReadings : (!(entity.Alarm16AlarmsOn ?? false) ? (notConfig : (reading.Value16 >= entity.Alarm16Critical ? (critical : (reading.Value16 >= entity.Alarm16Warning ? (warning : (ok)))
let Value17Status = !runSpeedOK || reading.Value17 == null ? (noReadings : (!(entity.Alarm17AlarmsOn ?? false) ? (notConfig : (reading.Value17 >= entity.Alarm17Critical ? (critical : (reading.Value17 >= entity.Alarm17Warning ? (warning : (ok)))
let Value18Status = !runSpeedOK || reading.Value18 == null ? (noReadings : (!(entity.Alarm18AlarmsOn ?? false) ? (notConfig : (reading.Value18 >= entity.Alarm18Critical ? (critical : (reading.Value18 >= entity.Alarm18Warning ? (warning : (ok)))
let OverallStatus = (new int[18] { Value1Status, Value2Status, Value3Status, Value4Status, Value5Status, Value6Status, Value7Status, Value8Status, Value9Status, Value10Status, Value11Status, Value12Status, Value13Status, Value14Status, Value15Status, Value16Status, Value17Status, Value18Status }).Max()
select new StatusModel() { Reading = reading, Status = (StatusValue)overallStatus }).ToList();

当然,这是另一种既是最好也是最干净的选择,而且我不知道为什么我之前没有想到它。显然是因为沮丧而蒙羞:

foreach (var reading in db.Readings.ToList())
{
    result.Add(new ReportVibReadingStatusModel() { Reading = reading, Status = reading.GetOverallStatusValue() });
}

1 个答案:

答案 0 :(得分:0)

我必须承认我并不过分熟悉实体框架,但我知道它支持存储过程。我会写一个存储过程来返回纯粹在SQL端确定的值。意思是所有那些ValueStatuses。

我可能会在评论中将此作为一个问题,但我还不能:)