5
  1. 我收到了一份 RDLC 报告
  2. 存储在 SQL DB 中的日期格式是格鲁吉亚语。我想在报告中显示日期为波斯语。
  3. 使用 Linq,我想选择 DB 的所有字段以及一些用作波斯日期字段的新字段。

我使用了以下语法:

    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
                    };

我也尝试了 select new {invoice,....} 但是它不会导致数据表与第一个相同。

4

2 回答 2

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

如果这不起作用,我建议先获取数据然后进行转换。

于 2012-04-05T22:33:11.570 回答
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 类型,您应该在转换之前注意它。

于 2012-04-06T07:27:24.293 回答