我正在尝试将linq写入实体查询,如下所示:
var lst = (from rw in ctx.Reviews
where rw.RefId == 10
&& rw.UpdatedTime == (from inner in ctx.CompareReviews
where inner.RefId == 10
&& inner.ReviewNumber == rw.ReviewNumber
select inner.UpdatedTime).Max()
select rw).ToList();
我希望它会生成以下查询:
select *
from review rw
where rw.ref_id = 10
and rw.updated_time = (select max(irw.updated_time)
from review irw
where irw.ref_id = 10
and irw.review_number = rw.review_number)
但事实并非如此,它会生成如下查询:
select rw.id,
/* .. other fields */
(
select max(irw.updated_time)
from review irw
where irw.ref_id = 10
and irw.review_number = rw.review_number
) as updated_time
from review rw
where rw.ref_id = 10
如何让实体框架生成第一个查询。
答案 0 :(得分:0)
请尝试以下查询:
SELECT id, <other_columns>
FROM review
WHERE ref_id = 10
ORDER BY updated_time DESC limit1;
注意:ref_id和updated_time字段应编入索引。
根据您的更新要求:
SELECT review_number,id, <other_columns>
FROM
(SELECT id,ref_id,<other_columns> FROM review
WHERE ref_id = 10
ORDER BY updated_time DESC) a
GROUP BY review_number;