如何解决如何使dbContext DBSet <Anyclass>方法获取给定实体的最后一个值
我已经创建了一种特定方法,该方法可以从产品表中检索最后一个[Code]值,我也想从其他表中检索代码,请建议我如何创建通用方法, 特定的班级代码。
public static string GetCode(SDMSDbContext dbContext)
{
string lastProductCode = string.Empty;
var LastProduct = dbContext.Products.AsNoTracking().OrderByDescending(x => x.Id).FirstOrDefault();
if (LastProduct != null)
lastProductCode = LastProduct.Code;
return lastProductCode.GenerateNextCode("PC");
}
和NextNumber代码。
public static string GenerateNextCode(this string code,string identifier)
{
if (string.IsNullOrEmpty(code))
return "${identifier}-000001";
var newProductCode = Convert.ToInt32(code.Split(new char[] { '-' })[1]) + 1;
return $"{identifier}-{newProductCode.ToString().PadLeft(6,'0')}";
}
我想要类似的东西。
public static string GetLastCode(SDMSDbContext sDMSDbContext,DbSet AnyClass)
{
_dbContext = sDMSDbContext;
string lastCode = string.Empty;
var LastProduct = _dbContext.entities.AsNoTracking().OrderByDescending(x => x.Id).FirstOrDefault();
if (LastProduct != null)
lastCode = LastProduct.Code;
return lastProductCode.GenerateNextCode("SO");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。