[英]Linq query to get the most recently inserted Id from a table
I am trying to get the most recently inserted id from the TaskOrder table to use as a hidden field for when I create subtasks.我正在尝试从 TaskOrder 表中获取最近插入的 id,以用作创建子任务时的隐藏字段。 My linq query is below and I am getting an anonymous type error that I can't figure out.
我的 linq 查询如下,我收到一个我无法弄清楚的匿名类型错误。
public async Task<int> GetLastTaskOrderId()
{
var result = (from t in _context.TaskOrder
select new
{
TaskorderId = t.TaskOrderId
}).MaxAsync();
return await result;
}
Try this尝试这个
public async Task<int> GetLastTaskOrderId()
{
var item = await _context.TaskOrder.OrderBy(u => u.TaskOrderId).LastOrDefaultAsync();
return item.TaskOrderId;
}
or或者
return await _context.TaskOrder.MaxAsync(u => u.TaskOrderId);
I don't know which is faster since the prime key is usually indexed and clustered.我不知道哪个更快,因为主键通常是索引和聚集的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.