Ejemplo n.º 1
0
        /// <summary>
        /// Groups list of stops by their trip short name, merges and sorts their stop times.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static List <StopDataModel> GroupByTripShortName(this List <StopDataModel> data)
        {
            if (data.First() == null)
            {
                return(new List <StopDataModel>());
            }

            List <StopDataModel> temp     = new List <StopDataModel>();
            StopDataModel        previous = null;

            foreach (string distinctedTripShortName in data.Select(x => x.TripShortName).Distinct())
            {
                foreach (StopDataModel item in data.Where(x => x.TripShortName == distinctedTripShortName))
                {
                    if (previous == null)
                    {
                        previous = item;
                        previous.TripHeadsign = "";
                        continue;
                    }
                    previous.DepartureTimes.AddRange(item.DepartureTimes);
                }
                previous.DepartureTimes = previous.DepartureTimes.OrderBy(x => x).ToList();
                temp.Add(previous);
                previous = null;
            }

            return(temp);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Maps table like list StopDataEntity into object like representation StopDataModel.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="stopName"></param>
        /// <returns></returns>
        public static List <StopDataModel> StopDataEntityToModel(this List <StopDataEntity> data, string stopName)
        {
            StopDataModel        previous   = null;
            List <StopDataModel> mappedData = new List <StopDataModel>();

            foreach (var item in data)
            {
                if (previous == null)
                {
                    previous = new StopDataModel()
                    {
                        StopLat        = item.StopLat,
                        StopLon        = item.StopLon,
                        HTMLColor      = item.HTMLColor,
                        StopName       = stopName,
                        TripShortName  = item.TripShortName,
                        TripHeadsign   = item.TripHeadsign,
                        DirectionID    = item.DirectionID,
                        RouteId        = item.RouteId,
                        RouteShortName = item.RouteShortName,
                        DepartureTimes = new List <string>()
                        {
                            item.DepartureTime.Trim()
                        }
                    };
                }
                else if (item.TripHeadsign != previous.TripHeadsign ||
                         item.TripShortName != previous.TripShortName ||
                         item.DirectionID != previous.DirectionID)
                {
                    mappedData.Add(previous);
                    previous = new StopDataModel()
                    {
                        StopLat        = item.StopLat,
                        StopLon        = item.StopLon,
                        HTMLColor      = item.HTMLColor,
                        StopName       = stopName,
                        TripShortName  = item.TripShortName,
                        TripHeadsign   = item.TripHeadsign,
                        DirectionID    = item.DirectionID,
                        RouteId        = item.RouteId,
                        RouteShortName = item.RouteShortName,
                        DepartureTimes = new List <string>()
                        {
                            item.DepartureTime.Trim()
                        }
                    };
                }
                else
                {
                    previous.DepartureTimes.Add(item.DepartureTime.Trim());
                }
            }
            mappedData.Add(previous);

            foreach (var item in mappedData)
            {
                if (item != null)
                {
                    item.DepartureTimes = item.DepartureTimes.OrderDepartureTimeAscending();
                }
            }

            return(mappedData);
        }