代码
public ActionResult ReturnPassengerCode(string ModelOfCar) { string returnPassengerCode = ""; try { var PassengerCodeType = from tbPassengerCar in myModals.SYS_PassengerCar group tbPassengerCar by tbPassengerCar.ModelOfCarID into tbModalOfCar select new { ModelIfCarID = tbModalOfCar.Key, PassengerCode = tbModalOfCar.OrderByDescending(m => m.PassengerCarID).FirstOrDefault().PassengerCarCode, }; var selectModelOfCar = (from tbPassengerCodeType in PassengerCodeType join tbModeOfCar in myModals.SYS_ModelOfCar on tbPassengerCodeType.ModelIfCarID equals tbModeOfCar.ModelOfCarID where tbModeOfCar.ModelOfCar == ModelOfCar select tbModeOfCar).ToList(); if (selectModelOfCar.Count>0) { int ModelOfCarID = 0; int ModelOfCarLength = 0; foreach (var item in selectModelOfCar) { ModelOfCarID = item.ModelOfCarID; ModelOfCarLength = item.ModelOfCar.Length; } var PassengerCode = (from tbPassengerCodeType in PassengerCodeType where tbPassengerCodeType.ModelIfCarID == ModelOfCarID select tbPassengerCodeType.PassengerCode).Single(); string passengerCodeEnd = PassengerCode.Substring(ModelOfCarLength); MatchCollection passengerCode = Regex.Matches(passengerCodeEnd, "\\d{1}"); int LastPassengerCode = Convert.ToInt32(passengerCode[4].Value); int FourPassengerCode = Convert.ToInt32(passengerCode[3].Value); int ThreePassengerCode = Convert.ToInt32(passengerCode[2].Value); int TwoPassengerCode = Convert.ToInt32(passengerCode[1].Value); int FirstPassengerCode = Convert.ToInt32(passengerCode[0].Value); if (LastPassengerCode