简体   繁体   中英

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

在此处输入图片说明

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",

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM