Home > Software design >  How to make query in blazor to get last ID from table
How to make query in blazor to get last ID from table

Time:02-26

I want get the last id from table. I'm using component blazor and i try do this:

int lastID = dl.db.myTable.FromSqlRaw("SELECT MAX(Id) FROM myTable");

I got the error CS0029: Cannot implicitly convert type 'System.Linq.IQueryable' to 'int'

CodePudding user response:

Try to use Linq

db.myTable.OrderByDescending(x => x.Id).FirstOrDefault();

CodePudding user response:

Your statement will return IQueryable and you can not store it into int datatype, so you can use

var list = dl.db.myTable.FromSqlRaw("SELECT MAX(Id) FROM myTable"); int lastID = list.FirstOrDefault().Value

  • Related