public IHttpActionResult GetDeptWiseCategories() { try { using (MaxMasterDbEntities db = new MaxMasterDbEntities()) { var overallCategories = db.GetCategories().ToList(); var departmentsInfo = overallCategories.GroupBy(x => x.DepartmentId).Select(x => x.First()).Select(x => new { x.DepartmentId, x.Department, Categories = overallCategories.Where(y => y.DepartmentId == x.DepartmentId).GroupBy(y => y.CategoryId).Select(y => y.First()).Select(y => new { y.CategoryId, y.CategoryName, SubCategories = overallCategories.Where(z => z.CategoryId == y.CategoryId).GroupBy(z => z.SubCategoryId).Select(z => z.First()).Select(z => new { z.SubCategoryId, Name = z.SubCategory, z.Points, z.PointsLogId }).OrderBy(z => z.Name).ToList() }).OrderBy(y => y.CategoryName).ToList() }).OrderBy(x => x.Department).ToList(); return(Content(HttpStatusCode.OK, new { departmentsInfo })); } } catch (Exception ex) { new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace); return(Content(HttpStatusCode.InternalServerError, "An error occured, please try again later")); } }