我使用EF6,我有这个linq到实体查询:
from s in SensorObservationEntities.SensorsMeasures
group s by s.SensorUnitId into g
let latest = g.OrderByDescending(s => s.MeasureDate).FirstOrDefault()
select latest
如何将其传递给非查询表达式?
答案 0 :(得分:2)
你的意思是方法语法:
SensorObservationEntities.SensorsMeasures.GroupBy(g => g.SensorUnitId)
.Select(y => y.OrderByDescending(x => x.MeasureDate).FirstOrDefault());
答案 1 :(得分:1)
如果要将其转换为方法语法版本,可以逐步执行此操作。我喜欢从最后开始,一直到最开始:
select
到Select
定义来源:
.Select(g => g.OrderByDescending(s => s.MeasureDate).FirstOrDefault());
group
是GroupBy
:
.GroupBy(s => s.SensorUnitId)
.Select(g => g.OrderByDescending(s => s.MeasureDate).FirstOrDefault());
from
来源
SensorObservationEntities.SensorsMeasures
.GroupBy(s => s.SensorUnitId)
.Select(g => g.OrderByDescending(s => s.MeasureDate).FirstOrDefault());