public IHttpActionResult GetLeadDetails(int leadId) { try { using (MaxMasterDbEntities db = new MaxMasterDbEntities()) { var leadFullDetails = db.GetLeadDetail(leadId).ToList(); var leadDetails = leadFullDetails.GroupBy(x => x.OppId).Select(x => x.First()).Select(x => new { x.OppId, x.OpportunityName, x.OppTaskOwner, OppStatus = x.OppCurrentStatus, x.CreatedDate, x.Client, x.LeadLocation, x.Description, TaskOwner = x.OppTaskOwner, CompletedDate = x.ActualCompletedDate, x.CreatedBy, oppLog = leadFullDetails.Where(y => y.OppId == x.OppId).GroupBy(y => y.OppLogId).Select(y => y.First()).Select(y => new { oppLogId = y.OppLogId, OppLogCreatedBy = y.OppLogAssignedBy, y.OppLogAssignedTo, y.OppLogComments, LogDate = y.OppLogDate, LogStatus = y.OppLogStatus, Attachments = leadFullDetails.Where(a => a.OppLogIdForAttachment == y.OppLogId).GroupBy(a => a.AttachmentId).Select(a => a.First()).Select(a => new { a.AttachmentId, a.FileAttachment }).ToList() }).ToList(), clientContacts = leadFullDetails.Where(z => z.OppId == x.OppId).GroupBy(z => z.ClientEmpId).Select(z => z.First()).Select(z => new { FirstName = z.ClientEmpFirstName, LastName = z.ClientEmpLastName, Email = z.ClientEmpEmail, PhoneNumber = z.ClientEmpPhoneNumber, Department = z.clientEmpDepartment }).ToList() }).FirstOrDefault(); return(Content(HttpStatusCode.OK, new { leadDetails })); } } catch (Exception ex) { new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.StackTrace, ex.ToString()); return(Content(HttpStatusCode.InternalServerError, "An error occured, please try again later")); } }