public string SaveAllNewRFQ(tbl_RFQHeader rfqObj) { using (var contex = new WorkFlowManagerDBEntities()) { var temp = ""; try { var first = contex.sp_GetMaxRFQDocNum().FirstOrDefault(); if (first != null) { temp = (int.Parse(first) + 1).ToString(CultureInfo.InvariantCulture); } } catch { temp = 0.ToString(CultureInfo.InvariantCulture); } rfqObj.DocNumber = temp; int c; try { c = (from a in (from x in contex.tbl_PurchaseOrderHeader where x.PurchaseID.StartsWith(rfqObj.BrandCode + "-" + rfqObj.SeasonCode) select x.PurchaseID).ToList() let str = SubstractString(a) let value = NullableTryParseInt32(str) select value ?? 0).Max(); } catch { c = 000000; } foreach (var purchaseOrderHeader in rfqObj.tbl_PurchaseOrderHeader) { var purchNum = ""; var newC = (c + 1); if (newC < 10) { purchNum = "00000" + newC.ToString(CultureInfo.InvariantCulture); } if (newC > 10 && newC < 100) { purchNum = "0000" + newC.ToString(CultureInfo.InvariantCulture); } if (newC > 100 && newC < 1000) { purchNum = "000" + newC.ToString(CultureInfo.InvariantCulture); } if (newC > 1000 && newC < 10000) { purchNum = "00" + newC.ToString(CultureInfo.InvariantCulture); } if (newC > 10000 && newC < 100000) { purchNum = "0" + newC.ToString(CultureInfo.InvariantCulture); } if (newC > 100000 && newC < 1000000) { purchNum = newC.ToString(CultureInfo.InvariantCulture); } purchaseOrderHeader.PurchaseID = (rfqObj.BrandCode + "-" + rfqObj.SeasonCode + "-" + (string.IsNullOrEmpty(purchNum) ? "000000" : purchNum)) ; c++; } contex.tbl_RFQHeader.AddObject(rfqObj); contex.SaveChanges(); return(rfqObj.DocNumber); } }