Search code examples
c#asp.net.netlinqclass-library

If From & To date is same record not filtering LINQ


I am filtering records based on Dates using Where clause but it's not working. From and To date is same & one record comes within the same date but that getting eliminates.

todate {11/6/2018 12:00:00 AM} //while debugging

if (toDate != "0")
{
    var todate = DateTime.Parse(toDate).Date;
    query = query.Where(x => x.CreatedDate.Date <= todate);
}
var homework = query.Select(x => new StudentHomework
{
    HomeworkId = x.HomeworkID,
    CreatedDate = x.CreatedDate
}).OrderByDescending(i => i.CreatedDate).Skip(pageLong * recordLong - recordLong).Take(recordLong);
return homework.Any()
    ? Request.CreateResponse(HttpStatusCode.OK, homework)
    : Request.CreateResponse(HttpStatusCode.NoContent);

From db coming one record to controller while debugging in VS

enter image description here

But query is showing 0 records & in ResulView getting below messages

Message = "The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

If I remove filter in my mobile app json result coming like this

"HomeworkId": 1400,
"ClassName": "Class 1",
"SectionName": "N/A",
"SubjectName": "Mathematics",
"IsAnswered": false,
"CreatedDate": "2018-11-06T00:00:00",

Solution

  • First of all feeling stupid to wasting time of everyone by asking confusing question. Here the issue was this line's .Date

    query = query.Where(x => x.CreatedDate.Date <= todate);
    

    In LINQ to Entities I should not have .Date and it is not allowing throwing Exception. After removing this everything working fine. I have try catch block but control was not jumping to it.