'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported

后端 未结 8 535
粉色の甜心
粉色の甜心 2020-11-27 05:57

I am trying to execute the following code and am receiving an error

public List GetLoggingData(DateTime LogDate, string title)
{
     var context          


        
8条回答
  •  借酒劲吻你
    2020-11-27 06:23

    Correct me if I'm wrong, but in mikemurf22's example, it would need to check each part of the date component, and potentially a lot more server processing?

    Anyway, I stumbled across this problem, and this is my solution.

    Assuming that you're going to be passing in the date component only, you can find the last minute of the day that you pass in, and use the where clause to define the range.

    public List GetLoggingData(DateTime LogDate, string title)
    {
        DateTime enddate = new DateTime(LogDate.Year, LogDate.Month, LogDate.Day, 23, 59, 59)
    
        var query = from t in context.Logs
                    where t.Timestamp >= date
                    where t.Timestamp <= enddate
                    select t;
    
        return query.ToList();
    }
    

提交回复
热议问题