我正在處理兩個表:CI_CLIENTRISK(SCD類型2)...和QB_INVOICES_HEADER(edmx screenshot)。以linq方法語法連接兩個表,MVC EntityFramework
它們可以通過ClientID連接。我想實質上覆制此查詢:
SELECT a.ClientID,
MAX(b.InvoiceDt) AS MaxInvoiceDt
(omitted for brevity)
FROM CI_CLIENTRISKADJS a
INNER JOIN QB_INVOICES_HEADER b
ON a.ClientID = b.ClientID
WHERE a.IsActive = 1
GROUP BY a.ClientID
ORDER BY MaxInvoiceDt DESC
這是我到目前爲止。它沒有返回任何記錄。
using (var db = new PLOGITENS01Entities())
{
var rvClientRiskAdjs = db.CI_CLIENTRISKADJS
.Take(50)
.Join(db.QB_INVOICES_HEADER,
a => a.ClientID,
b => b.ClientID,
(a, b) => new { Risk = a, Invoices = b })
.Where(a => a.Risk.IsActive == 1)
.OrderByDescending(o => o.Invoices.InvoiceDt)
.Select(c => new ClientRiskModel()
{
ClientRiskId = c.Risk.ClientRiskID,
ClientName = c.Risk.CI_CLIENTLIST.ClientName,
ClientId = c.Risk.ClientID,
ClientRiskAdjs = c.Risk.ClientRiskAdjs,
RecordValidStartDt = c.Risk.RecordValidStartDt,
RecordValidEnddt = c.Risk.RecordValidEnddt,
IsActive = c.Risk.IsActive
})
.ToList();
return View(new GridModel(rvClientRiskAdjs));
}
謝謝你的提示。 :)這是有效的,但它沒有考慮最大聚合(在SQL查詢的第二行中找到)。它返回所有客戶端ID和沒有組的列表。我最終偶然發現了這篇文章的答案:http://stackoverflow.com/a/157919/1689144 – Kyle