2016-02-03 56 views
0

我有這樣的查詢:如何在包含表達式中選擇一個?

return db.Tanks 
     .Include(i => i.FluedLevelSensor) 
     .Include(i => i.WaterLevelSensor) 
     .Include(i => i.GasPressureSensor) 
     .Include(i => i.HudrostaticPressureSensor) 
     .Include(i => i.TechnologicalMaps) 
     .Include(i => i.TechnologicalGroup) 
     .Include(i => i.TemperatureSensor).ThenInclude(t => t.TemperatureSensorPoints) 
     .Include(i => i.Material) 
     .Include(i => i.TankType) 
     .Include(i => i.QualityPassports).ThenInclude(i1 => i1.ContentType) 
     .Include(i=>i.CalibrationTableNew) 
     .Include(i => i.TankOperations) 
     .Include(i => i.TankStates).ThenInclude(i => i.State); 

我怎麼能寫這樣的:

return db.Tanks 
    .Include(i => i.FluedLevelSensor) 
    .Include(i => i.WaterLevelSensor) 
    .Include(i => i.GasPressureSensor) 
    .Include(i => i.HudrostaticPressureSensor) 
    .Include(i => i.TechnologicalMaps.OrderByDescending(o=>o.InsertTime).FirstOrDefault()) 
    .Include(i => i.TechnologicalGroup) 
    .Include(i => i.TemperatureSensor).ThenInclude(t => t.TemperatureSensorPoints) 
    .Include(i => i.Material) 
    .Include(i => i.TankType) 
    .Include(i => i.QualityPassports.OrderByDescending(o => o.SamplingDate).FirstOrDefault()).ThenInclude(i1 => i1.ContentType) 
    .Include(i=>i.CalibrationTableNew) 
    .Include(i => i.TankOperations.OrderByDescending(o => o.OperationStartTime).FirstOrDefault()) 
    .Include(i => i.TankStates.OrderByDescending(o => o.Time).FirstOrDefault()).ThenInclude(i => i.State); 

以及是否有任何其他的方法來優化查詢?

+0

豈不是更容易禁用延遲加載此背景下? – wentimo

回答

0

如果你不能啓用急切加載,你可以利用任何東西選擇是由定義包括

注:那年代由實體框架發出的SQL可以是相同的:

return db.Tanks.Select(i => new { 
    /* single easy value */ 
    Material = i.Material, 

    /* for collections, select again */ 
    TankStates = i.TankStates.OrderByDescending(s => s.Time).ToList(), 

    /* ... and so on */ 
}); 
相關問題