2010-09-16 233 views
0

我想把SQL查詢複製到LINQ中。 任何人都可以幫忙嗎?將SQL查詢轉換爲LINQ查詢

SQL:

SELECT tblInvoice.lngID AS InvoiceID, 
       tblInvoice.dtTimeStamp AS InvoiceDate, 
       tblInvoice.strReference, 
       tblInvoice.fltTotalValue, 
       max(Project.ProjectID) AS ProjectID, 
       max(Project.ProjectName) AS ProjectName, 
       max(Project.Location) AS ProjectLocation 
     FROM tblInvoice INNER JOIN 
       tblInvoiceLine ON tblInvoice.lngID = tblInvoiceLine.lngInvoiceID 
     WHERE (tblInvoice.intStatus != 0) 
     AND  (tblInvoice.lngPersonID = @PersonID) 
     GROUP BY tblInvoice.lngID, tblInvoice.dtTimeStamp, strReference, fltTotalValue 
     ORDER BY tblInvoice.lngID DESC 

LINQ至今:

問題的
var invoices = from inv in db.TblInvoices 
    join invLine in db.TblInvoiceLines on inv.LngID equals invLine.LngInvoiceID 
    where inv.IntStatus != 0 
    where inv.LngPersonID == personID 
    group inv by new {inv.LngID,inv.DtTimeStamp,inv.StrReference,inv.FltTotalValue} into newInv 

的是,我想要做一個

select new Invoice(){ 
} 

,並建立我的自定義發票的對象,但,我無法看到newInv中的任何屬性。

任何人都可以建議嗎?

回答

1

我沒有時間爲一個完整的答案了,但:

  • 要獲取關鍵的屬性,使用newInv.Key.StrReference
  • 要獲取聚合體(如最大值)使用newInv.Max(x => x.ProjectId)

希望這足以讓你走。基本上,newInv將是一個的條目,與相關的(這是你分組的)。

+0

感謝那個喬恩我昨天有點腦凍結了。 – DazManCat 2010-09-17 09:13:30

+0

當然,其他選項是做一個簡單的LINQ查詢,然後做一個invoices.GroupBy(x => new {x.InvoiceID,x.InvoiceDate,x.Reference,x.TotalValue}); – DazManCat 2010-09-17 09:24:47