public List <ShippingLoadData> GetLoadsForHomeTab(Guid identUserId) { List <string> loadshopLoadTypes = new List <string> { TransactionTypes.New , TransactionTypes.Updated }; var userPrimaryCustomerId = _context.Users.SingleOrDefault(u => u.IdentUserId == identUserId)?.PrimaryCustomerId; if (!userPrimaryCustomerId.HasValue || userPrimaryCustomerId.Equals(Guid.Empty)) { throw new Exception($"Unable to determine primary customer ID for IdentUserId {identUserId}"); } var eligibleTransactionTypes = new List <string> { TransactionTypes.New, TransactionTypes.Updated, TransactionTypes.PreTender, TransactionTypes.Pending, TransactionTypes.PendingAdd, TransactionTypes.PendingUpdate, TransactionTypes.PendingFuel, TransactionTypes.PendingRates, TransactionTypes.PendingRemoveScac }; var loads = _context.Loads .Include(x => x.LoadStops) .Include(x => x.Customer) .Include(x => x.Equipment) .Include(x => x.LatestLoadTransaction) .Include(x => x.LoadServiceTypes) .Where(x => x.CustomerId == userPrimaryCustomerId && eligibleTransactionTypes.Contains(x.LatestTransactionTypeId)) .ToList(); // order by loads that are in marketplace and then the pickup date relative to today var orderedLoadTransactions = loads .OrderBy(_ => loadshopLoadTypes.Contains(_.LatestTransactionTypeId) ? 1 : 0) .ThenBy(l => _serviceUtilities.DistanceFromToday(l.LoadStops.Where(ls => ls.StopNbr == 1).SingleOrDefault()?.LateDtTm ?? _dateTime.Now)); return(_mapper.Map <List <ShippingLoadData> >(orderedLoadTransactions)); }