我对 EF5 没有太多经验,我的 DBA 生成的查询非常复杂,而且我在工具的选择上非常有限。我将描述问题。数据库未规范化,因此此类查询是必要的。而且我不能影响它的结构和查询例如我有查询:
SELECT A.my_id,
A.field_1,
A.field_2,
A.field_3,
A.field_4,
A.field_5,
A.field_6,
A.field_7,
A.field_8,
A.field_9,
B.field_10,
C.field_11,
C.field_12,
C.field_13,
C.field_14,
D.field_15,
E.my_id2,
E.field_16,
E.field_17,
E.field_18,
G.field_19
etc...
FROM tbl1 A,
tbl2 B, tbl3 C, tbl4 D, tbl5 E, tbl6 F, tbl7 G
WHERE A.my_id = B.my_id
AND A.my_id = C.my_id
AND A.my_id = D.my_id
AND A.my_id = E.my_id
AND E.my_id3 = F.my_id3
AND E.my_id4 = G.my_id4
AND E.my_id2 = <value from code>
AND F.param1 = <value from code>
AND B.param4 = 'KEY1'
AND B.param5 = 'KEY2'
AND E.param8 > 0
AND E.param9 = 'KEY3'
AND E.param10 <> 'KEY4'
AND G.param11 = 'KEY5'
AND G.param12 <= <SYSTEM DATETIME>
AND G.param13 >= <SYSTEM DATETIME>
AND A.param2 <= <SYSTEM DATETIME>
AND A.param3 >= <SYSTEM DATETIME>
AND B.param6 <= <SYSTEM DATETIME>
AND B.param7 >= <SYSTEM DATETIME>`
现在我将描述使用 Entity Framework 5 在代码中移动查询时遇到的问题。
我对主表进行 INNER JOIN。像这样。
ObjectContext.CreateQuery<ETbl5>(entityName).
.Where(E =>
E.param2 >= SqlFunctions.GetDate() && E.param3 <= SqlFunctions.GetDate() &&
E.param8 > 0 && E.param9 == "KEY3" &&
E.param10 != "KEY4" && E.my_id2 == <value from code>
)
.Join((Context as MyDbContext).ETbl7 ,
E => new { A.my_id4},
G => new { B.my_id4},
(E, G) => E
)
如何为 JOIN 添加条件AND G.param12 <= <SYSTEM DATETIME>
?如何在匿名类型中添加此类条件?
我总是有很多问题,我认为我走错了路。或许除了解决问题,你还可以告诉我处理此类查询的正确策略。我知道它可以转移到存储过程中,但是我希望输出 IQuerable 以在 Grid with Pagination 中使用它。感谢您的关注。