Grizzly
Grizzly

Reputation: 5943

LINQ to Entities does not recognize the method ' '

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?

Upvotes: 0

Views: 37

Answers (1)

John Boker
John Boker

Reputation: 83709

You could try something like

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

Upvotes: 1

Related Questions