public async Task <IEnumerable <dynamic> > GetAllAssignedPositions(string sourcemarket, string jobfamily, string positiontype, string criteria) { if (sourcemarket == "null") { sourcemarket = "ALL"; } if (jobfamily == "null") { jobfamily = "ALL"; } if (positiontype == "null") { positiontype = "ALL"; } string[] SourceMarkets = sourcemarket.Split(','); string[] JobFamilys = jobfamily.Split(','); string[] PositionTypes = positiontype.Split(','); var rawdata = await _positionAssignRepo.GetAssignedPositions(SourceMarkets, sourcemarket, JobFamilys, jobfamily, PositionTypes, positiontype); var rawlist = rawdata.ToList(); if (!string.IsNullOrEmpty(criteria)) { rawlist = FilterByCriteria(criteria, rawlist).ToList(); } var groupedBySeason = rawlist.GroupBy(x => x.SDD_DM) .Select(x => new { Season = x.Key, HeadOfs = x.ToList().GroupBy(y => y.Destination).Select(y => new { HeadOf = y.Key, Destinations = y.ToList().GroupBy(m => m.JobTitle) .Select(m => new { Destination = m.Key, Positions = m.ToList() .Select(n => new { id = n.Id, PositionAssignId = n.PositionAssignId, firstName = n.FirstName, firstNameLastName = n.FirstName + n.LastName, lastName = n.LastName, staffID = n.StaffID, mplID = n.MPLID, mplIDVersion = n.MPLIDVersion, profile = n.Profile, languages = n.Languages, education = n.Education, highseason = n.HighSeason, mPLPositionType = n.MPLPositionType, mplDlRequired = n.MPL_DL_Required, nat = n.Nat, headof = n.HeadOf, concepthotel = n.ConceptHotel, jobTitle = n.JobTitle, sourceMarket = n.SourceMarket, mplSourceMarket = n.MPLSourceMarket, startDate = n.StartDate, accept = n.Accept, season = n.Season, acting = n.Acting, endDate = n.EndDate, positionStartDate = n.PositionStartDate, email = n.Email, positionEndDate = n.PositionEndDate, driver = n.Driver, drivingYear = n.DrivingYear, status = n.Status, positionType = n.PositionType }).ToList().OrderBy(o => o.lastName) }).OrderBy(j => j.Destination) } ).OrderBy(d => d.HeadOf) }).OrderBy(h => h.Season); return(groupedBySeason); }