此LINQ查询不支持OUTER APPLY

时间:2016-09-06 13:39:20

标签: c# oracle linq

我需要在oracle 11.2数据库中运行此查询,但生成的查询包含“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
    };

1 个答案:

答案 0 :(得分:0)

OUTER APPLY由您的let语句表达式生成。您可以使用基于EXISTS的等效LINQ结构将它们转换为Any可翻译表达式来避免它:

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,因为{{1}}导航属性暗含了这一点。