简体   繁体   中英

How to make query in blazor to get last ID from table

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<myTable>' to 'int'

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

Try to use Linq

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

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