我有一个SQL子选择查询,它将检索父记录的最新历史记录。
我无法将以下内容转换为LINQ:
SELECT *
FROM ProductRequests INNER JOIN ProductRequestHistory
ON ProductRequests.ID = ProductRequestHistory.ProductRequestsID
AND ProductRequestHistory.ID = (SELECT TOP 1 ProductRequestHistory.ID
FROM ProductRequestHistory
WHERE ProductRequestHistory.ProductRequestsID = ProductRequestHistory.ID
ORDER BY ProductRequestHistory.DateCreated DESC)
这是我尝试过的,但它没有编译:
from productrequests in db.ProductRequests
join productrequesthistories in db.ProductRequestHistories
on new { productrequests.ID, Column1 = (Int32?)Convert.ToInt32(
((from productrequesthistories0 in db.ProductRequestHistories
where
productrequesthistories0.ProductRequestsID == productrequesthistories0.ID
orderby
productrequesthistories0.ID descending
select new {
productrequesthistories0.ID
}).Take(1).First().ID)) }
equals new { ID = (System.Int32?)productrequesthistories.ProductRequestsID, Column1 = productrequesthistories.ID }
select new {
productrequests.ID,
productrequests.ProductRequestNumber,
productrequests.ProjectID,
Column1 = productrequesthistories.ID,
productrequesthistories.Title,
productrequesthistories.Requester,
productrequesthistories.TFSNumber,
productrequesthistories.UseCase,
productrequesthistories.RequestType,
productrequesthistories.FunctionalModule,
productrequesthistories.Description,
productrequesthistories.Reason,
productrequesthistories.Priority,
productrequesthistories.Status,
productrequesthistories.Release,
productrequesthistories.Estimate,
productrequesthistories.Wags,
productrequesthistories.Confidence,
productrequesthistories.Notes,
productrequesthistories.RequirementStatus,
productrequesthistories.RequirementDoco,
productrequesthistories.AlphaRequired,
productrequesthistories.Sprint,
productrequesthistories.Created,
productrequesthistories.DateChanged,
productrequesthistories.Checksum,
productrequesthistories.ProductRequestsID
}
答案 0 :(得分:3)
这在 VB.NET
中Dim result = From request in ProductRequests
Join request2 in ProductRequestHistory
On request.ID = request2.ProductRequestsID
And request2.ID = (From request3 in ProductRequestHistory
Where request3.ProductRequestsID = request3.ID
Order by request3.DateCreated desc
Select request3.ID).First()
Select New With {
.request = request
.history = request2
}
在 C#
中var result = (from request in db.ProductRequests
Join request2 in ProductRequestHistory
On request.ID == request2.ProductRequestsID
&& request2.ID == (from request3 in ProductRequestHistory
where request3.ProductRequestsID == request3.ID
orderby request3.DateCreated desc
select request3.ID).First()
select new {
request = request
history = request2
}
)
这应该可以解决问题。
另外,在原始的SQL语句中,我很想改变
AND ProductRequestHistory.ID = (SELECT TOP 1 ProductRequestHistory.ID ......
部分为WHERE
子句,因为JOIN
没有必要。
因此,在LINQ中你可以使用
Where request2.ID = (From request3 in ProductRequestHistory ......
此外,您可以优化查询预先计算TOP 1 ID
值:
top1id = (From request3 in ProductRequestHistory
Where request3.ProductRequestsID = request3.ID
Order by request3.DateCreated desc
Select request3.ID).First()
然后使用Where request2.ID = top1id
。
答案 1 :(得分:0)
如果我没弄错的话:
var rusult = ProductRequests.Join(ProductRequestHistory,
p => p.ID,
d => d.ProductRequestsID,
(p, d) => p).where(w => w.Id == ProductRequestHistory.OrderByDescending(o=>o.DateCreated).FirstOrDefault(s=>s.ProductRequestsID == s.ID));