从db读取不同车辆的最后记录,并在视图中使用它们 [英] Read last records from db for different vehicles and use them on view

查看:108
本文介绍了从db读取不同车辆的最后记录,并在视图中使用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

所以我正在创建驱动程序/车辆跟踪应用程序,但我在这方面遇到了问题。



所以我应该阅读车辆的最后记录,其中存储的数据显示车辆是否开启(车辆状态是真还是假)。



这部分是为了显示哪个车辆在线atm或5分钟前,(我仍然不确定从传感器获得的信号之间会有多长时间延迟)



我尝试过:



所以我试过这个

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);
        }





其中,VehicleUsages是用于存储所有车辆在一段时间间隔内的数据的表,其中包括车辆的ids和驾驶员,油位,行驶速度等以及车辆状态。



我还发现这个查询会提取VehicleOnOff(State)为真的最新数据,所以没有如果atm在线,它将为​​我提供车辆上线的最新数据。



如何查询列表以便我可以查看哪辆车在线?



提前谢谢。



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.

推荐答案

Sooo ...你有一些递增的ID号或者这些记录上的日期/时间戳告诉你哪个记录是最后一个插入数据库的记录,对吗?没有它就不能这样做。
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.


最后我实际做的是这个。



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);





我刚刚在Razor View中查看了





And I just checked it in Razor View

@foreach (var veh in Model.OnlineVehicles)
                {

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


这篇关于从db读取不同车辆的最后记录,并在视图中使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆