Home > front end >  LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method
LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method

Time:10-07

In my asp.net MVC application, view model I have created a property type string to display date.

But in the model, the property is stored as DateTime, so in my query, I have assigned the DateTime by converting it to ToShortDateString.

Because the value stored is with the DateTime in the table.(example2022-10-06 11:32:48.917)

But in the view, I just want to show the Date only.

When running this query I got this error

LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, 
and this method cannot be translated into a store expression.'

Just want to know how to pass only the date to the view of this kind of query.

This is my current code.

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();

CodePudding user response:

ToShortDateString() is not something that the database recognize. So you need to bring the conversion to client side by using .AsEnumerable() (or .ToList()). Try this code.

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();
  • Related