在我的 asp.net MVC 應用程式中,查看模型我創建了一個屬性型別string來顯示date。
但是在模型中,該屬性存盤為DateTime,因此在我的查詢中,我通過將 DateTime 轉換為 來分配它ToShortDateString。
因為存盤的值是表中的 DateTime。(示例2022-10-06 11:32:48.917)
但在視圖中,我只想顯示日期。
運行此查詢時出現此錯誤
LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method,
and this method cannot be translated into a store expression.'
只想知道如何只將日期傳遞給這種查詢的視圖。
這是我當前的代碼。
var TaskMain = (from t in db.TaskMain
join service in db.Services on t.Service_Id equals service.Id
join category in db.ServiceCategory on service.Service_Category_Id equals category.Id
join branch in db.Branch on t.Current_Branch_Id equals branch.Id
join emp in db.Employee on t.Task_Assigned_Emp_Id equals emp.Id
where t.Id == id
select new TaskDetails
{
Id = t.Id,
Note = t.Note,
Current_Step= t.Task_Step_Id,
Service_Category = category.Service_Category_Name,
Service_End_Date = t.Service_End_Date.ToShortDateString(),
Service_Price = t.Service_Price,
Service_Start_Date = t.CreatedDate.ToShortDateString(),
Task_Created_Branch = branch.BranchName,
Service_Name = service.Service_NameEng
}).ToList();
uj5u.com熱心網友回復:
ToShortDateString()不是資料庫識別的東西。.AsEnumerable()因此,您需要使用(或.ToList())將轉換帶到客戶端。試試這個代碼。
var TaskMain = (from t in db.TaskMain
join service in db.Services on t.Service_Id equals service.Id
join category in db.ServiceCategory on service.Service_Category_Id equals category.Id
join branch in db.Branch on t.Current_Branch_Id equals branch.Id
join emp in db.Employee on t.Task_Assigned_Emp_Id equals emp.Id
where t.Id == id
select new
{
Id = t.Id,
Note = t.Note,
Task_Step_Id= t.Task_Step_Id,
Service_Category_Name = category.Service_Category_Name,
Service_End_Date = t.Service_End_Date,
Service_Price = t.Service_Price,
CreatedDate = t.CreatedDate,
BranchName = branch.BranchName,
Service_NameEng = service.Service_NameEng
}).AsEnumerable()
.Select(t => new TaskDetails
{
Id = t.Id,
Note = t.Note,
Current_Step= t.Task_Step_Id,
Service_Category = t.Service_Category_Name,
Service_End_Date = t.Service_End_Date.ToShortDateString(),
Service_Price = t.Service_Price,
Service_Start_Date = t.CreatedDate.ToShortDateString(),
Task_Created_Branch = t.BranchName,
Service_Name = t.Service_NameEng
}).ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511419.html
