简体   繁体   English

从数据库返回最后一条记录

[英]Returning last record from database

I am trying to display the last record that was processed according to the database, a client has processes that belongs to them.我试图显示根据数据库处理的最后一条记录,客户端有属于他们的进程。

So Client 1 has processes 1 & 2, and I am looking to display what the last record and when it was processed according to the process table in the database.所以客户端 1 有进程 1 和 2,我希望根据数据库中的进程表显示最后一条记录的内容以及处理时间。 I want to return this to a view and display in a table.我想将其返回到视图并显示在表格中。

Here is what I have been trying so far:这是我迄今为止一直在尝试的:

 var robots = from h in _context.HeartBeat
                         join p in _context.ProcessTable on h.ProcessID equals p.ProcessID
                         join e in _context.EnvironmentTable on p.EnvironmentID equals e.EnvironmentID
                         join c in _context.ClientTable on e.ClientID equals c.ClientID
                         select new DashViewModel
                         {
                             processName = p.ProcessID,
                             environmentName = e.EnvironmentName,
                             LastRunTime = h.RecordFinish
                         };


            IEnumerable<DashViewModel> robo = robots.ToList<DashViewModel>();

this selects all the processes from database.这将从数据库中选择所有进程。 there is thousands so this isnt ideal.有数千个所以这并不理想。

Assuming you are using EF Core you could use:假设您使用的是 EF Core,您可以使用:

var last = robots.Last();

to get the last record.获取最后一条记录。 Or或者

var last3 = robots.TakeLast(3).ToList();

to get a certain number form the end.从最后得到一定的数字。 You may want to force a certain sort with .OrderBy(r => r.Id) or something.您可能想使用.OrderBy(r => r.Id)或其他东西强制某种排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM