Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,
so I'm in creation of driver/vehicle tracking application, but I'm having issues on this part.

So I'm supposed to read last record of vehicle, where data is stored which shows if vehicle is on or off (VehicleState true or false).

This part is meant to show which vehicle is online atm or 5 mins ago,(I'm still unsure how long will delay be between signals I'll get from sensors)

What I have tried:

So I've tried with this
public ActionResult Index()
        {
            var userId = User.Identity.GetUserId();

            var online = context.VehicleUsages.Where(m => m.AccountId == userId && m.VehicleOnOff == true)
                .GroupBy(p => p.VehicleId)
                .Select(p => p.FirstOrDefault(w => w.Id == p.Max(m => m.Id)))
                .OrderBy(p => p.VehicleId)
                .Include(p => p.Vehicle)
                .Include(p => p.Driver)
                .Include(p => p.Vehicle.VehicleTypes)
                .ToList();

            var viewModel = new DashboardViewModel
            {
                OnlineVehicles = online
            };

            return View(viewModel);
        }


Where VehicleUsages is table used for storing data of all vehicles over some time interval which include, ids of vehicle and driver, fuel level, movement speed etc. and vehicle state.

I also figured out this query will pull out latest data where VehicleOnOff(State) is true, so no matter if atm its online it will bring me up latest data where vehicle was online.

How can I query it to list so I can show on View which car is online?

Thanks in advance.
Posted
Updated 9-Aug-17 23:10pm
v2

Sooo...you've got some incrementing ID number or date/time stamp on these records to tell you which record was the last one inserted into the database, correct? You can't do this without it.
 
Share this answer
 
Comments
Faris Karcic 8-Aug-17 4:02am    
Yeah as the matter of fact I have both
Dave Kreskowiak 8-Aug-17 8:23am    
Sort the records by that field in descending order, like you've already done with the VehicleId, and put a .Take(1) on the end of the query.

Faris Karcic 10-Aug-17 5:07am    
It didnt quite do it, but at least you helped me think with more logic. I'll accept it! Thanks.
In the end what I actually did was this.

var online = context.VehicleUsages.Where(m => m.AccountId == userId)
                .GroupBy(p => p.VehicleId)
                .Select(p => p.FirstOrDefault(w => w.Id == p.Max(m => m.Id)))
                .OrderBy(p => p.VehicleOnOff)
                .Include(p => p.Vehicle)
                .Include(p => p.Driver)
                .Include(p => p.Vehicle.VehicleTypes)
                .ToList();
                

            var viewModel = new DashboardViewModel
            {
                OnlineVehicles = online
            };

            return View(viewModel);


And I just checked it in Razor View

@foreach (var veh in Model.OnlineVehicles)
                {

                if (veh.VehicleOnOff == true)
                    {
                    <!--html code here-->
                        
                    }
                
                }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900