0

I am trying to pull the ID from the latest record in the table where the foreign key matches what is being entered by the user.

When I try and do this, I receive this error:

LINQ to Entities does not recognize the method 'ALogSummary.Models.MaintenanceRecord LastOrDefaultMaintenanceRecord' method, and this method cannot be translated into a store expression.

Here is my line of code where the error occurs:

int latestID = db.MaintenanceRecords.Where(x => x.AID == dailySummary.AID).LastOrDefault().ID;

I have also tried:

int latestID = db.MaintenanceRecords.LastOrDefault(x => x.AID == dailySummary.AID).ID;

But that does not work either.

What needs to be modified so I can achieve this?

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • http://stackoverflow.com/questions/7293639/linq-to-entities-does-not-recognize-the-method-last-really – John Boker May 05 '16 at 17:22
  • [Supported and Unsupported LINQ Methods (LINQ to Entities)](https://msdn.microsoft.com/en-us/library/bb738550(v=vs.110).aspx) – Ivan Stoev May 05 '16 at 17:36

1 Answers1

1

You could try something like

int latestID = db.MaintenanceRecords
         .Where(x => x.AID == dailySummary.AID) 
         .OrderByDescending(x => x.AID)
         .FirstOrDefault().ID;
John Boker
  • 82,559
  • 17
  • 97
  • 130