【问题标题】:LINQ to Entities does not recognize the method 'System.String ToShortDateString()' methodLINQ to Entities 无法识别方法 \'System.String ToShortDateString()\' 方法
【发布时间】:2022-10-07 14:08:04
【问题描述】:

在我的 asp.net MVC 应用程序中,查看模型我创建了一个属性类型string 来显示date

但在模型中,该属性存储为DateTime,因此在我的查询中,我通过将日期时间转换为ToShortDateString 来分配它。

因为存储的值是与表中的日期时间。(example2022-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();

    标签: c# asp.net asp.net-mvc entity-framework linq


    【解决方案1】:

    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,
                      Current_Step= 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(s => 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 2013-04-07
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多