OUTER APPLY in .net 4.5 and Entity Framework

After installing .Net 4.5 , problems arose in requests generated by EF. The same query in linq to .Net 4.0 works correctly. I use the Firebird database and do not support the OUTER APPLY command. Is there a way to modify the following linq request so that it behaves in .Net 4.5 ?

Current linq request:

 int[] AROIds = (from x in context.RISK_T_ASSESS_HIST where (x.ID_RISKOBJECT.HasValue && x.F_CREATEDON >= Freq.StartDate && x.F_CREATEDON <= Freq.EndDate) group x by x.ID_RISKOBJECT into gr let lastCreated = gr.Max(p => p.F_CREATEDON) select new { ObjId = gr.Key , LastStatus = gr.Where(p => p.F_CREATEDON == lastCreated && p.ID_RISKOBJECT == gr.Key) .Select(p => p.F_STATUS).FirstOrDefault() }).Where(x => x.LastStatus == 0 || x.LastStatus == 1) .Select(x => x.ObjId.Value).ToArray(); 

Generated by SQL.NET 4.5:

 SELECT "Project1"."ID_RISKOBJECT" AS "ID_RISKOBJECT" FROM (SELECT "GroupBy1"."A1" AS "C1", "GroupBy1"."K1" AS "ID_RISKOBJECT" FROM ( SELECT "Extent1"."ID_RISKOBJECT" AS "K1", MAX("Extent1"."F_CREATEDON") AS "A1" FROM "RISK_T_ASSESS_HIST" AS "Extent1" WHERE (("Extent1"."ID_RISKOBJECT" IS NOT NULL) AND ("Extent1"."F_CREATEDON" >= 2013-04-17)) AND ("Extent1"."F_CREATEDON" <= 2013-04-17) GROUP BY "Extent1"."ID_RISKOBJECT" ) AS "GroupBy1" ) AS "Project1" OUTER APPLY (SELECT FIRST (1) "Extent2"."F_STATUS" AS "F_STATUS" FROM "RISK_T_ASSESS_HIST" AS "Extent2" WHERE (((("Extent2"."ID_RISKOBJECT" IS NOT NULL) AND ("Extent2"."F_CREATEDON" >= 2013-04-17)) AND ("Extent2"."F_CREATEDON" <= @2013-04-17)) AND (("Project1"."ID_RISKOBJECT" = "Extent2"."ID_RISKOBJECT") OR (("Project1"."ID_RISKOBJECT" IS NULL) AND ("Extent2"."ID_RISKOBJECT" IS NULL)))) AND (("Extent2"."F_CREATEDON" = "Project1"."C1") AND ("Extent2"."ID_RISKOBJECT" = "Project1"."ID_RISKOBJECT")) ) AS "Limit1" WHERE (0 = "Limit1"."F_STATUS") OR (1 = "Limit1"."F_STATUS") 
+4
source share
1 answer

Ok The only solution I found while reading in the original list table. Unfortunately, not quite what I had in mind, but it works.

  AIds = (from x in ( context.RISK_T_ASSESS_HIST.Where(x => x.ID_RISKOBJECT.HasValue && x.F_CREATEDON >= Freq.StartDate && x.F_CREATEDON <= Freq.EndDate) .Select(x => new { x.ID_RISKOBJECT, x.F_CREATEDON, x.F_STATUS }).ToList() ) group x by x.ID_RISKOBJECT into gr let lastCreated = gr.Max(p => p.F_CREATEDON) select new { ObjId = gr.Key , LastStatus = gr.Where(p => p.F_CREATEDON == lastCreated && p.ID_RISKOBJECT == gr.Key).Select(p => p.F_STATUS).FirstOrDefault() }).Where(x => x.LastStatus == 0 || x.LastStatus == 1).Select(x => x.ObjId.Value).ToArray(); 

This is an Entity Framework issue and will be reported as an error on their forum.

+1
source

Source: https://habr.com/ru/post/1485956/


All Articles