如何在LINQ中选择所有字段和一些新字段

本文关键字:字段 新字段 LINQ 选择 | 更新日期: 2023-09-27 18:30:06

  1. 我收到RDLC报告
  2. SQL数据库中存储的日期格式为格鲁吉亚语。我想将报告中的日期显示为波斯语
  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
                    };

我还尝试选择了新的{发票,……},但它不会产生和第一个相同的数据表。

如何在LINQ中选择所有字段和一些新字段

你可以通过所有的字段,但这并不漂亮,最好通过发票本身,而不是它的字段:

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

p.S:我不确定这些行在linq2entities中是否能正常工作(有些日子我没有任何IDE,我忘记了一切:):

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

您稍后可以访问类似的字段

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

日期时间铸造也起作用。你应该注意发票。DateCreation在强制转换之前是可为null的类型。