自动生成编号(B开头后跟6位,数据库查询不重复)

时间:2022-03-23 15:15:16
 private string GetAccountNo()
{
try
{
string shortName="B";
string latestAccountNO = shortName + "";
            //查询数据库 7位且包含“B”
var templist = twRep.GetAll().Where(x => x.AccountNo.Length == && x.AccountNo.Contains(shortName)).OrderByDescending(x => x.AccountNo).ToList();
if (templist.Count>)
{
             //取第一个
string latestNO = templist.FirstOrDefault().AccountNo;             //截取后六位
latestAccountNO = latestNO.Substring(, );
             //判断是否能转换整形
int iNewNo = ;
int.TryParse(latestAccountNO,out iNewNo);
if (iNewNo > )
{
latestAccountNO = shortName + (iNewNo + ).ToString("");
}
else
{
latestAccountNO = shortName + "";
} }
return latestAccountNO;
}
catch (Exception ex)
{
           //错误信息日志方法
LogHelper.WriteLog(ex.Message, ex);
return "";
} }