Linq Exception: Function can only be invoked from linq to entities

Viewed 22918

I have a StudentReceipts table which stores ReceiptNo as string(001,002,003,..,099,..).

I want go get the last receiptno details inorder to increment the receiptno for next transaction.

This is what I have tried

  var _lastGeneratedRecDetails = _db.StudentReceipts
                                 .AsEnumerable()
                                 .Where(r => r.Status == true
                                             && EntityFunctions.TruncateTime(r.DueDate.Value) >= _startDate.Date
                                             && EntityFunctions.TruncateTime(r.DueDate.Value) <= _endDate.Date)                                                
                                            .OrderByDescending(x => Int32.Parse(x.ReceiptNo))
                                            .FirstOrDefault();

But there i am getting the following exception

this function can only be invoked from linq to entities

Any help will be highly appreciated.

3 Answers

In my case, I was re-using a Func / Filter expression that included DbFunctions.TruncateTime in a follow-up processing statement AFTER I had already processed the query in SQL. Removing it cleared the instance of the exception for me.

use and .AsQueryable()

var _lastGeneratedRecDetails = _db.StudentReceipts
                             .AsEnumerable().AsQueryable()
Related