1

I am developing an asp.net MVC system with a Kendo UI. I have to send a "date" from a filter button in View to the controller and filter the LINQ. I used this code:

public ActionResult Grid_ReadLogAdminList([DataSourceRequest] DataSourceRequest request, string filterDate)
        {
            DateTime _temp;
            if (!DateTime.TryParse(filterDate, out _temp))
                _temp = DateTime.Now;

            return Json(_context.Entities<LogAdmin>().NoTracking().OrderByDescending(l => l.TimeStamp)
                .Where(f => f.TimeStamp.Value.ToString("dd.MM.yyyy") == _temp.ToString("dd.MM.yyyy"))
                .Select(l => new LogAdminInfo
                {
                    Id = l.Id,
                    Message = l.Message,
                    MessageTemplate = l.MessageTemplate,
                    Level = l.Level,
                    TimeStamp = l.TimeStamp,
                    Exception = l.Exception,
                    Properties = l.Properties,
                    LogEvent = l.LogEvent,
                })
                .ToDataSourceResult(request));
        }

but it gave me an error with the ".Where". you should know that TimeStamp field is "datetime?" nullable datetime.

I received this Error:

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

how can I fix the Error?

5
  • You need to first check for your date is null or not. Commented Oct 11, 2016 at 13:34
  • I tried to do it. .Where(f => (f.TimeStamp != null ? f.TimeStamp.Value.ToString("dd.MM.yyyy") : "") == _temp.ToString("dd.MM.yyyy")) .... did not work Commented Oct 11, 2016 at 13:35
  • 2
    Because it couldn't be translated to T-SQL, Linq to Entities couldn't recognize it. See here:stackoverflow.com/a/34061692/2946329 Commented Oct 11, 2016 at 13:35
  • first select your field and do ToString in the select close, then apply the where. Select clause is in .NET Field where as Where is in SQL field Commented Oct 11, 2016 at 13:37
  • Use [DisplayFormat(ApplyFormatInEditMode= true, DataFormatString = "{0:dd.MM.yyyy}")] public DateTime? TimeStamp { get; set; } at your ViewModel Property Commented Oct 11, 2016 at 13:37

3 Answers 3

1

The error is pretty indicative LINQ to Entities does not support the ToString method, you should compare your dates using another approach.

Based on your code I assume you are only interested in the date part of the DateTime for comparison so I suggest you to try DbFunctions.TruncateTime Method:

Where(f => DbFunctions.TruncateTime(f.TimeStamp) == DbFunctions.TruncateTime(_temp))
Sign up to request clarification or add additional context in comments.

Comments

1

replace

.Where(f => f.TimeStamp.Value.ToString("dd.MM.yyyy") == _temp.ToString("dd.MM.yyyy"))

with

.Where(f =>  DbFunctions.TruncateTime(f.TimeStamp) == _temp)

Comments

0
    var tempDate = _temp.ToString("dd.MM.yyyy")    
    Json(_context.Entities<LogAdmin>().NoTracking().OrderByDescending(l => l.TimeStamp)    
.Select(l => new LogAdminInfo
                    {
                        Id = l.Id,
                        Message = l.Message,
                        MessageTemplate = l.MessageTemplate,
                        Level = l.Level,
                        TimeStamp = l.TimeStamp.Value.ToString("dd.MM.yyyy"),
                        Exception = l.Exception,
                        Properties = l.Properties,
                        LogEvent = l.LogEvent,
                    }).Where(l => l.TimeStamp = tempDate).Select(l)
                    .ToDataSourceResult(request));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.