Ejemplo n.º 1
0
        public List<HomeViewModel> CreateHomeViewModel(string pickedDate)
        {
            var items = new List<HomeViewModel>();
            ICollection<Ship> ships = ships = db.Ships.ToList();
            var list = db.Journeys.Where(p => p.Ship.State)
                .GroupBy(p => p.ShipId).ToList().Select(m => new { journey = m.OrderByDescending(g => g.JourneyId).FirstOrDefault() });
            if (pickedDate != null && pickedDate != string.Empty)
            {
                DateTime pickedTimecv = DateTime.ParseExact(pickedDate, "dd/MM/yyyy", null);
                foreach (var item in list)
                {
                    try
                    {
                        string pickedDateS = pickedTimecv.Date.ToString();
                        var jns = db.JourneySuppervision.ToList().OrderByDescending(p=>p.Id)
                            .FirstOrDefault(p => p.ShipId == item.journey.ShipId && p.Date==pickedDateS);
                        var a = new HomeViewModel
                        {
                            ShipName = item.journey.Ship.ShipName,
                            StartDate = item.journey.StartDate,
                            TripId = item.journey.TripId,
                            ShipId = item.journey.ShipId,
                            FromTo = item.journey.StartNationName + " / " + item.journey.StopNationName,
                            CurPosAndState = jns==null?"": jns.StateId != null ? (jns.Position + " | " + jns.State.StateName) : (jns.Position + " | ")
                        };
                        items.Add(a);
                    }
                    catch (Exception)
                    {
                    }
                }
                return items.OrderBy(p=>p.ShipName).ToList();
            }
            else
            {
                foreach (var item in list)
                {
                    try
                    {
                        var jns = db.JourneySuppervision.OrderByDescending(p => p.Id).FirstOrDefault(p => p.ShipId == item.journey.ShipId && p.TripId == item.journey.TripId);
                        var a = new HomeViewModel
                        {
                            ShipName = item.journey.Ship.ShipName,
                            StartDate = item.journey.StartDate,
                            TripId = item.journey.TripId,
                            ShipId = item.journey.ShipId,
                            FromTo = item.journey.StartNationName + " / " + item.journey.StopNationName,
                            CurPosAndState = jns == null ? "" : jns.StateId != null ? (jns.Position + " | " + jns.State.StateName) : (jns.Position + " | ")
                        };
                        items.Add(a);
                    }
                    catch (Exception)
                    {
                    }
                }
                return items.OrderBy(p => p.ShipName).ToList();
            }

            //var list = db.Journeys.Join(db.JourneySuppervision, p => p.ShipId, m => m.ShipId, (m, p) => p);
            //foreach (var item in list)
            //{

            //}
        }
Ejemplo n.º 2
0
 public List<HomeViewModel> GetHomeViewModelByTime(string start, string stop)
 {
     DateTime startTime = string.IsNullOrEmpty(start) ? DateTime.MinValue : DateTime.ParseExact(start, "dd/MM/yyyy", null);
     DateTime stopTime = string.IsNullOrEmpty(stop) ? DateTime.Now : DateTime.ParseExact(stop, "dd/MM/yyyy", null);
     var list = db.Journeys.Where(p => (p.StartDate >= startTime && (p.StartDate <= stopTime || p.EndDate == null)) || (p.StartDate <= startTime && (p.EndDate >= startTime || p.EndDate == null))).ToList();
     var items = new List<HomeViewModel>();
     ICollection<Ship> ships = ships = db.Ships.ToList();
     foreach (var item in list)
     {
         var name = ships.FirstOrDefault(p => p.ShipId == item.ShipId).ShipName;
         var a = new HomeViewModel { ShipName = name, StartDate = item.StartDate, TripId = item.TripId, ShipId = item.ShipId, FromTo = item.StartNationName + " / " + item.StopNationName };
         items.Add(a);
     }
     return items;
 }