我使用了以下语法:
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
};
我也试过选择新的{invoice,....}但是它不会导致数据表与第一个相同。
答案 0 :(得分: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
};
P.S:我不确定这些行在linq2entities中是否正常工作(有些日子我没有任何IDE而忘记了所有内容:)
PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0
如果这不起作用,我建议首先获取数据,然后进行转换。
答案 1 :(得分: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类型,您应该小心。