如何在Linq To Entities中格式化日期

时间:2013-08-05 15:43:12

标签: linq linq-to-entities

我需要在我的LINQ To Entities查询中将datetime格式化为shortdatestring。我尝试了以下方法,但它不起作用 -

aryData =
(
    From cert In db.LWCerts _
    Select New With { _
       .ToBeProcessedDate = cert.ToBeProcessedDate.ToShortDateString 
    }
).ToArray()

如何将日期作为短日期字符串返回(“即:08/05/2013”​​)?

编辑这是整个linq查询 -

             aryData =
        (From lwl In db.LWCertLoans _
        Join c In db.Loans _
   On c.LoanNum Equals lwl.LoanNum _
   Join p In db.LWCertColls _
   On lwl.CertID Equals p.CertID _
   Join r In db.RespCntrs _
   On r.BranchNum Equals c.BranchNum _
   Join cert In db.LWCerts.AsEnumerable() _
   On cert.LWCertID Equals lwl.CertID _
   Where lwl.LoanNum = p.LoanNum _
   Select New With { _
       .ToBeProcessedDate = cert.ToBeProcessedDate, _
    .CertType = cert.CertType, _
    .CertCollID = p.CertCollID, _
    .CertificateID = p.CertID, _
    .LoanNumberTypeAndCurrencyCombined = c.LoanNum, _
    .LoanType = c.LoanType, _
    .CurrType = r.CurrType, _
    .CollanteralBalance = c.ColCurBal, _
    .SalesAdditions = p.Sales, _
    .CreditMemos = p.Credits, _
    .CashRemovals = p.NetCollect, _
    .NonDilutiveAdjustment = p.PlusAdj, _
    .Discounts = p.Discounts, _
    .NonARCash = p.NonARCash, _
    .DilutiveAdjustment = p.NegAdj, _
    .LWCertCollsComments = p.Comments, _
    .StatusCode = p.StatusCode, _
    .CertLoanID = lwl.CertLoanID, _
    .Modified = lwl.Modified, _
    .LoanNum = lwl.LoanNum, _
    .EffectiveDate = lwl.EffectiveDate, _
    .RepWireNumber = lwl.RepWireNumber, _
    .Advance = lwl.Advance, _
    .ModifiedDate = lwl.ModifiedDate, _
    .DDAAccountName = lwl.DDAAccountName, _
    .LWCertLoansComments = lwl.Comments, _
    .Comment = If(cert.CertType = 0, p.Comments, lwl.Comments)}).ToArray()

1 个答案:

答案 0 :(得分:1)

您应该使用ToString

AsEnumerable()从数据库移动到应用程序
aryData =
(
    From cert In db.LWCerts.AsEnumerable() _
    Select New With { _
       .ToBeProcessedDate = cert.ToBeProcessedDate.ToShortDateString 
    }
).ToArray()

这将使DateTime原样来自数据库,然后在应用程序端进行格式化。