Convert datetime to a formatted string inside a LINQ-to-entities query

前端 未结 6 899
时光取名叫无心
时光取名叫无心 2020-12-13 19:15

How can I convert DateTime into a formatted string?

This is the line in the following query that needs help:

StartDate = string.Format(\         


        
6条回答
  •  不思量自难忘°
    2020-12-13 19:31

    if it's a datetime you need to use the .ToShortDateString(). But you also need to declare it AsEnumerable().

    var offer = (from p in dc.CustomerOffer.AsEnumerable()
                     join q in dc.OffersInBranch
                         on p.ID equals q.OfferID
                     where q.BranchID == singleLoc.LocationID
                     let value = (p.OriginalPrice - p.NewPrice) * 100 / p.OriginalPrice
                     orderby value descending
                     select new
                     {
                         Title = p.OfferTitle,
                         Description = p.Description,
                         BestOffer=value,
                         ID=p.ID,
                         LocationID=q.BranchID,
                         LocationName=q.CustomerBranch.BranchName,
                         OriginalPrice=SqlFunctions.StringConvert((decimal)p.OriginalPrice),
                         NewPrice=SqlFunctions.StringConvert((decimal)p.NewPrice),
                         StartDate=p.StartDate
    
                     })
                     .ToList()
                     .Select(x => new Offer()
                     {
                         Title = x.OfferTitle,
                         Description = x.Description,
                         BestOffer=value,
                         ID=x.ID,
                         LocationID=x.BranchID,
                         LocationName=x.CustomerBranch.BranchName,
                         OriginalPrice=x.OriginalPrice,
                         NewPrice=x.NewPrice,
                         StartDate=x.StartDate.ToShortDateString()
                     }).First();
    

提交回复
热议问题