Tôi biết rằng LINQ to SQL không chuyển đổi DateTime
thành string
vì không có ToString()
trong SQL. Nhưng làm cách nào tôi có thể chuyển đổi DateTime
thành chuỗi được định dạng?Chuyển đổi datetime thành chuỗi được định dạng bên trong truy vấn LINQ to SQL
Đây là dòng trong truy vấn sau đây cần giúp đỡ:
StartDate = string.Format("{0:dd.MM.yy}", p.StartDate)
Toàn bộ truy vấn:
var offer = (from p in dc.CustomerOffer
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 Offer()
{
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 = string.Format("{0:dd.MM.yy}", p.StartDate)
}).First();
tôi nhận được thông báo lỗi sau:
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
Lỗi: LINQ to Entities không nhận ra phương thức 'System.String ToString (System.String)' và phương thức này không thể được dịch sang biểu thức cửa hàng. – kandroid
Vì vậy, là p.StartDate một DateTime hoặc một chuỗi? – mccow002
p.startdate -is datetime – kandroid