async Task<string> GenerateTestResultRefNo(int SampleId) { var Location = _authenticationManager.GetAuthenticatedUser().Result.LabLocation; var ReportRefNo = string.Empty; var MaxJobRefNo = 0; int JobLabId = _dbContext.TblSample.Where(k => k.Id == (int)SampleId).Select(k => (int)k.LabId).FirstOrDefault(); string CountryCode = _dbContext.MstLabLocation.Where(k => k.Id == JobLabId).Select(k => k.CountryCode).FirstOrDefault(); string LabCode = _dbContext.MstLabLocation.Where(k => k.Id == JobLabId).Select(k => k.LabCode).FirstOrDefault(); string StateCode = _dbContext.MstLabLocation.Where(k => k.Id == JobLabId).Select(k => k.StateCode).FirstOrDefault(); string MergeReportRefNo ="IGI:"+ CountryCode + LabCode + ":TR:" + ApplicationData.AppDateToYear(ApplicationData.DefaultDateTime).ToString().Substring(2, 2) + ":"; try { var SampleRefNoList = (from TblSampleLog in _dbContext.TblSampleReportHeader.Where(x => x.LabId == JobLabId) select new { ReportNo = TblSampleLog.ReportNo }).ToList(); if (SampleRefNoList.ToList().Count == 0) { ReportRefNo = MergeReportRefNo + "00001"; // IGI:INMUM:TR:23:00001 } else { var Max = SampleRefNoList.AsEnumerable().Select(k => new { SampleRefNo = int.Parse(k.ReportNo.Substring(16, 5)) }).Max(x => x.SampleRefNo); MaxJobRefNo = Max + 1; var MaxTransmitalString = "00000" + MaxJobRefNo.ToString(); var GenerateNo = MaxTransmitalString.Substring(MaxTransmitalString.Length - 5, 5); //SampleRefNo = "INMUM" + "/" + GenerateNo.ToString().Trim(); ReportRefNo = MergeReportRefNo + GenerateNo.ToString().Trim(); } } catch (Exception ex) { _logger.Error(ex.Message); ReportRefNo = string.Empty; } return await Task.Run(() => { return ReportRefNo; }); }