这是我的控制器自动编号
[HttpPost]
public ActionResult Index (updown viewModel)
{
var number = db.updowns.OrderByDescending(x => x.tanggal).Select(z => z.tanggal).FirstOrDefault() ?? "00000";
var lastPO = int.Parse(number.Substring(5, 5));
item.tanggal = "EMOC-" + (++lastPO).ToString("00000") + "-S2/" + DateTime.Now.Year;
db.updowns.Add(item);
db.SaveChanges();
}
我想在新的一年之后,号码00000
将再次重复00001
例如2016年的记录
EMOC-00001-S2/2016
EMOC-00002-S2/2016
EMOC-00003-S2/2016
并且在新年之后将再次将该号码重新编号为00001
EMOC-00001-S2/2017
EMOC-00002-S2/2017
EMOC-00003-S2/2017
任何人都可以帮助我,请用代码编写,以便让我更加了解..
抱歉我的英文不好答案 0 :(得分:0)
类似这样的事情
[HttpPost]
public ActionResult Index (updown viewModel)
{
using (var dbContextTransaction = db.BeginTransaction())
{
var year = DateTime.Now.Year("D4");
var lastPO = db.updowns
.Where(x => (x=> x.tangal.Substring(22 /* is it 22 ? */).Equals(year))
.OrderByDescending(x => x.tanggal)
.FirstOrDefault()?.tanggal.Substring(5, 5) ?? "00000";
item.tanggal = "EMOC-" + (lastPO+1).ToString("00000")
+ "-S2/" + DateTime.Now.Year;
db.updowns.Add(item);
db.SaveChanges();
}
}