如何将linq查询转换为非查询表达式?

时间:2016-07-06 13:46:54

标签: c# entity-framework linq

我使用EF6,我有这个linq到实体查询:

   from s in SensorObservationEntities.SensorsMeasures
            group s by s.SensorUnitId into g
            let latest = g.OrderByDescending(s => s.MeasureDate).FirstOrDefault()
            select latest

如何将其传递给非查询表达式?

2 个答案:

答案 0 :(得分:2)

你的意思是方法语法:

SensorObservationEntities.SensorsMeasures.GroupBy(g => g.SensorUnitId)
             .Select(y => y.OrderByDescending(x => x.MeasureDate).FirstOrDefault());

答案 1 :(得分:1)

如果要将其转换为方法语法版本,可以逐步执行此操作。我喜欢从最后开始,一直到最开始:

  1. selectSelect定义来源:

    .Select(g => g.OrderByDescending(s => s.MeasureDate).FirstOrDefault());
    
  2. groupGroupBy

    .GroupBy(s => s.SensorUnitId)
    .Select(g => g.OrderByDescending(s => s.MeasureDate).FirstOrDefault());
    
  3. from来源

    SensorObservationEntities.SensorsMeasures
    .GroupBy(s => s.SensorUnitId)
    .Select(g => g.OrderByDescending(s => s.MeasureDate).FirstOrDefault());