2011-07-23 195 views
0

我真的很陌生,所以我希望有人能幫助我。我有一個數據庫,我需要運行一個大型查詢,但它是一個非常古老的ODBC驅動程序,需要很長時間才能響應(即使是簡單的查詢也需要30分鐘以上)。將所有數據轉儲到數據集只需要大約2-3分鐘,所以我認爲這是最好的,然後我可以運行LINQ to Dataset查詢。我似乎無法得到查詢工作,我有點困惑。我將所有數據放入一個SQL Express數據庫中,以測試LINQ to SQL查詢,以確保我正沿着正確的路線前進。由於環境總是不同,我沒有這個選項將運行應用程序。LINQ to DataSet查詢幫助

SQL:

SELECT Invoice_detail.Code, Invoice_detail.Description, Product_master.Comment AS Packing, Invoice_detail.QtyInv AS INV, Invoice_detail.QtyBackOrder AS BO, Alternate_product_codes.MasterBarCode AS BarCode, Invoice_detail.PriceAmt AS Price, Invoice_detail.DiscPerc AS Disc, ROUND(Invoice_detail.TaxableAmt/Invoice_detail.QtyInv,2) AS Nett FROM ((Invoice_detail INNER JOIN Product_master ON Invoice_detail.Code = Product_master.Code) INNER JOIN Invoice_header ON Invoice_detail.InternalDocNum = Invoice_header.InternalDocNum AND Invoice_detail.DocType = Invoice_header.DocType) LEFT JOIN Alternate_product_codes ON Invoice_detail.Code = Alternate_product_codes.Code WHERE Invoice_header.DocNum = '{0}' AND Invoice_header.DocType = 1 AND Invoice_detail.LineType = 1 AND Invoice_detail.QtyInv > 0 

LINQ到SQL:

from detail in INVOICE_DETAILs 
join prodmast in PRODUCT_MASTERs on detail.Code equals prodmast.Code 
join header in INVOICE_HEADERs on new { detail.InternalDocNum, detail.DocType } equals new { header.InternalDocNum, header.DocType} 
join prodcodes in ALTERNATE_PRODUCT_CODES on detail.Code equals prodcodes.Code into alt_invd 
from prodcodes in alt_invd.DefaultIfEmpty() 
where 
    header.DocType == 1 && 
    detail.LineType == 1 && 
    detail.QtyInv > 0 && 
    header.Date > DateTime.Parse("17/07/2011").Date && 
    header.DocNum.Trim() == "119674" 
select new { 
    detail.Code, 
    detail.Description, 
    Packing = prodmast.Comment, 
    INV = detail.QtyInv, 
    BO = detail.QtyBackOrder, 
    Barcode = prodcodes.MasterBarCode, 
    Price = detail.PriceAmt, 
    Disc = detail.DiscPerc, 
    Nett = Math.Round(Convert.ToDecimal(detail.TaxableAmt/detail.QtyInv),2,MidpointRounding.AwayFromZero) 
} 

LINQ到數據集:

var query = from detail in ds.Tables["Invoice_detail"].AsEnumerable() 
join prodmast in ds.Tables["Product_master"].AsEnumerable() on detail["Code"] equals prodmast["Code"] 
join header in ds.Tables["Invoice_header"].AsEnumerable() on new { docnum = detail["InternalDocNum"], doctype = detail["DocType"] } equals new { docnum = header["InternalDocNum"], doctype = header["DocType"] } 
join prodcodes in ds.Tables["Alternate_product_codes"].AsEnumerable() on detail["Code"] equals prodcodes["Code"] into alt_invd 
from prodcodes in alt_invd.DefaultIfEmpty() 
where 
    (int)header["DocType"] == 1 && 
    (int)detail["LineType"] == 1 && 
    (int)detail["QtyInv"] > 0 && 
    //header.Field<DateTime>("Date") > DateTime.Parse("17/07/2011").Date && 
    header.Field<DateTime>("Date") > DateTime.Now.Date.AddDays(-7) && 
    header.Field<string>("DocNum").Trim() == "119674" 
select new 
{ 
    Code = detail["Code"], 
    Description = detail["Description"], 
    Packing = prodmast["Comment"], 
    INV = detail["QtyInv"], 
    BO = detail["QtyBackOrder"], 
    Barcode = prodcodes["MasterBarCode"], 
    Price = detail["PriceAmt"], 
    Disc = detail["DiscPerc"], 
    Nett = Math.Round(Convert.ToDecimal((double)detail["TaxableAmt"]/(int)detail["QtyInv"]), 2, MidpointRounding.AwayFromZero) 
}; 

我需要運行LINQ到數據集查詢,然後把結果放到一個DataTable,以便我可以導出爲CSV。該查詢將返回許多行,所以我可以看到CopyToDataTable方法,但似乎不工作,除非它是一個類型化的數據集。我正在使用ODBC數據適配器填充方法,因此沒有專門設置我正在填充的數據表的數據類型。原因是這些表中有很多列,並且將它們全部設置爲耗時。

是LINQ最好的選擇嗎?我關門了嗎?我是否必須爲所有列和數據類型設置DataTable?我能想到的唯一方法是每次將數據轉儲到訪問數據庫並從那裏查詢。我更加好奇讓LINQ工作,但我認爲這對我前進會更有益。

任何幫助或指針表示讚賞。

謝謝。

皮特。

回答

0

如果我理解正確的話,在Linq到數據集查詢檢索正確的信息,但是你是不是能夠將信息導出爲CSV。

如果這只是一個csv文件,只需使用示例中的九個字段進行創建,則可以使用csv庫(例如FileHelpers)導出信息。

爲了讓你所產生的額外工作的一個例子,你需要定義一個類如

[DelimitedRecord(",")] 
public class Info 
{ 
    [FieldQuoted()] 
    public string Code ; 
    [FieldQuoted()] 
    public string Description ; 
    [FieldQuoted()] 
    public string Packing ; 
    public decimal INV ; 
    public decimal BO ; 
    [FieldQuoted()] 
    public string Barcode ; 
    public decimal Price ; 
    public decimal Disc ; 
    public decimal Nett ; 

} 

(注意,我猜一些字段類型)

然後你就改變了查詢中使用的信息,即

select new Info { 
    Code = detail["Code"], 
... 

最後

FileHelperEngine engine = new FileHelperEngine(typeof(Info)); 
    engine.WriteFile(".\\outputfile.csv", query); 

你就完成了。