2012-04-05 80 views
5
  1. 我得到了RDLC報告
  2. 存儲在SQL DB中的日期格式是格魯吉亞語。我想在報告中顯示日期爲波斯語。
  3. 使用Linq我想選擇數據庫的所有字段,另外還有一些新的字段用作波斯日期字段。

我用下面的語法:如何選擇所有字段加上LINQ中的一些新字段?

var invoices = from invoice in dbContext.eve_Invoices 
        select new 
        { 

         invoice.CreatorID, 
         invoice.DateChange, 
         invoice.DateCreation, 
         invoice.DatePrint, 
         invoice.Discount, 
         invoice.DiscountPercentage, 
         invoice.DiscountType, 
         invoice.Fare, 
         invoice.ID, 
         invoice.InvoiceStatus, 
         invoice.NumberOfItems, 
         invoice.Packaging, 
         invoice.PrintID, 
         invoice.RawPrice, 
         invoice.RoundOff, 
         invoice.ServiceCharge, 
         invoice.ServingType, 
         invoice.TableID, 
         invoice.Tax, 
         invoice.TotalPrice, 
         invoice.ValidityStatus, 
         PersianYear = invoice.DateCreation != null ? pc.GetYear((DateTime)invoice.DateCreation) : 0, 
         PersianMonth = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0, 
         PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0 
        }; 

有任何的方式來傳遞發票的各個領域,除了新的領域,如:

var invoices = from invoice in dbContext.eve_Invoices 
        select new 
        { 

         invoice.*, 
         PersianYear = invoice.DateCreation != null ? pc.GetYear((DateTime)invoice.DateCreation) : 0, 
         PersianMonth = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0, 
         PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0 
        }; 

我也嘗試選擇新的{發票,....}但是它不會導致數據表與第一個數據表相同。

+0

你的第一個語法不會工作,你應該爲你的匿名類型命名參數,我也認爲DateTime的鑄造不會在這裏工作。 – 2012-04-05 22:35:28

回答

5

您可以通過所有的精密組件,但它不是漂亮,最好是通過發票本身,而不是它的領域:

var invoices = from invoice in dbContext.eve_Invoices 
       select new 
       { 
        RelatedInvoice = invoice, 
        PersianYear = invoice.DateCreation != null ? pc.GetYear((DateTime)invoice.DateCreation) : 0, 
        PersianMonth = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0, 
        PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0 
       }; 

PS:我不知道這行linq2entities認真工作(它在有些時候我沒有任何的IDE,我什麼都忘了:):

PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0 

如果這不工作,我建議首先獲取數據,然後做轉換。

+0

感謝您的幫助。主要的問題是我想要這個查詢的結果注入數據到RDLC報告,我不知道如何將發票數據類型轉換爲基本數據類型,可以在RDLC報告中用作基本數據類型。據我所知,RDLC不支持數據類型,如發票:( – VSB 2012-04-06 14:02:53

+0

linq2entities沒有辦法,除了使用類似於你的第一個示例,但linq2object反射,但我想要一個骯髒的方式,但我想正常的方式,首先我會從數據庫中獲取數據,然後我會將它們轉換爲例如UI格式。 – 2012-04-06 14:06:24

0

您可以使用發票本身。

var invoices = from invoice in dbContext.eve_Invoices 
       select new 
       { 
       invoice, 
       PersianYear = invoice.DateCreation != null ? >pc.GetYear((DateTime)invoice.DateCreation) : 0, 
       PersianMonth = invoice.DateCreation != null ? >pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0, 
       PersianDay = invoice.DateCreation != null ? >pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0 
       }; 

以後可以訪問這樣

foreach (var invc in invoices) 
    { 
    //invc.invoice.CreatorID 
    } 

日期時間鑄造等領域也適用。如果invoice.DateCreation在投射之前爲Nullable類型,您應該小心。

+0

感謝您的幫助。主要問題是我想要將此查詢的結果注入到RDLC報告中,不知道如何將發票數據類型轉換爲可用作RDLC報表中基本數據類型的基本數據類型。據我所知,RDLC不支持數據類型,如發票:( – VSB 2012-04-06 14:01:48

相關問題