Basit Nisar

Basit Nisar

  • 1.6k
  • 84
  • 10.8k

Optimization of COde

Sep 18 2023 10:25 AM

{ ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal; //Int32 LogedInID = Convert.ToInt32(principal.Claims.FirstOrDefault(t => t.Type == "LogedInID").Value); string usertype = principal.Claims.FirstOrDefault(t => t.Type.Contains("role")).Value.ToString(); ReleaseNoteViewModel ReleaseNote = new ReleaseNoteViewModel(); AdjusterReleaseNoteViewModel AdjReleaseNote = new AdjusterReleaseNoteViewModel(); var ReleaseVersionvw = db.ReleaseVersions.AsNoTracking().Where(x => x.ReleaseVersionID == ID).OrderBy(x => x.ReleaseVersionName); try { #region Adjuster's ReleaseNote Data if (usertype == "Adjuster") { AdjReleaseNote.AdjReleaseView = ReleaseVersionvw.Select(x => new AdjusterReleaseNoteModel { ReleaseVersionID = x.ReleaseVersionID, ReleaseVersionName = x.ReleaseVersionName, IsCurrentVersion = x.IsCurrentVersion, Adjuster = db.UserLoginProfiles.Where(a => a.UserLoginProfileID == 2).Select(s => new ReleaseItemModel { Improvement = s.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 2 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription }).ToList(), Feature = s.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 1 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription }).ToList() }).FirstOrDefault(), }).ToList<AdjusterReleaseNoteModel>(); return Ok(AdjReleaseNote); } #endregion else if (usertype == "CatStaff") { ReleaseNote.ReleaseNoteView = ReleaseVersionvw.Select(x => new ReleaseNoteModel { ReleaseVersionID = x.ReleaseVersionID, ReleaseVersionName = x.ReleaseVersionName, IsCurrentVersion = x.IsCurrentVersion, Staff = db.UserLoginProfiles.Where(s => s.UserLoginProfileID == 3).Select(s => new ReleaseItemModel { Improvement = s.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 2 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription, }).ToList(), Feature = s.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 1 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(p => new ReleaseNotesModel { ReleaseNoteID = p.ReleaseNoteID, ReleaseNoteHeader = p.ReleaseNoteHeader, ReleaseNoteDescription = p.ReleaseNoteDescription }).ToList() }).FirstOrDefault(), Adjuster = db.UserLoginProfiles.Where(a => a.UserLoginProfileID == 2).Select(s => new ReleaseItemModel { Improvement = s.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 2 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription }).ToList(), Feature = s.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 1 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription }).ToList() }).FirstOrDefault(), Client = db.UserLoginProfiles.Where(t => t.UserLoginProfileID == 1).Select(t => new ReleaseItemModel { Improvement = t.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 2 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription } ).ToList(), Feature = t.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 1 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription }).ToList() }).FirstOrDefault(), }).ToList<ReleaseNoteModel>(); } } catch (Exception ex) { Global.InsertException(ex); } 

here there is repitition of code is there a way to optimize it?


Answers (8)