2

親レコードの最新の履歴レコードを取得する 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
}
4

2 に答える 2

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

于 2012-07-03T13:48:30.113 に答える
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));
于 2012-07-03T13:58:22.347 に答える