2016-09-06 70 views
1

我需要在Oracle 11.2數據庫中運行此查詢,但生成的查詢包含「OUTER APPLY」。我該如何解決它?此LINQ查詢中不支持OUTER APPLY

var query = from r in Ctx.Reg 
    let status_1 = (r.Hist.OrderByDescending(o => o.Id).FirstOrDefault(h => h.RegId == r.Id).Status == 1) 
    let status_2 = (r.Hist.OrderByDescending(o => o.Id).Skip(1).FirstOrDefault(h => h.RegId == r.Id).Status == 2) 
    select new 
    { 
     r.Id, 
     ... 
     status_1, 
     status_2 
    }; 

回答

0

OUTER APPLY是由您的let語句表達式生成的。您可以通過使用等效Any基於LINQ結構使它們成爲EXISTS翻譯的表情避免:

var query = from r in Ctx.Reg 
    let status_1 = r.Hist.OrderByDescending(h => h.Id).Take(1).Any(h => h.Status == 1) 
    let status_2 = r.Hist.OrderByDescending(h => h.Id).Skip(1).Take(1).Any(h => h.Status == 2) 
    select new 
    { 
     r.Id, 
     ... 
     status_1, 
     status_2 
    }; 

注意,不需要h.RegId == r.Id因爲由r.Hist導航屬性暗示。