如何获取使用Linq从表中插入到实体的最新日期 [英] How to get the latest date inserted from the table with Linq to entities
问题描述
如何从列中检索检查的最新日期
how to retrieve the latestdate inspected from a column
在我们的应用程序中,用户可以检查项目,并且在检查项目时将当前日期输入到表中.对于同一物品,这一次其他人可以取消检查,而无需在表中插入任何物品. 另一个用户可以检查它并插入当前日期 我需要插入LatestDate ..如何使用Linq来获取它
In our app a user can inspect an item and when it inspected the current date is entered into the table. For the same item someother can uninspect it this time nothing is inserted into the table. Another user can inspect it and current date is inserted I need to get the LatestDate Inserted..how to get this with Linq
假设表名是状态",列名是"LastdateInspected"
Let say table name is Statuses and col name is LastdateInspected
推荐答案
听起来像您想要的东西:
Sounds like you want something like:
var query = db.Statuses
.OrderByDescending(x => x.LastDateInspected)
.FirstOrDefault();
如果没有条目,则将返回null
,否则将返回具有最新LastDateInspected
值的值(即,第一个结果,在最后检查的日期之前检查了最新优先).
That will return null
if there are no entries, or the value with the latest LastDateInspected
value otherwise (i.e. the first result, having ordered by the last date inspected latest-first).
当然,这将为您带来整个记录.如果只需要日期,则可以选择LastDateInspected
列.
That will get you the whole record, of course. If you only want the date, you can select the LastDateInspected
column.
这篇关于如何获取使用Linq从表中插入到实体的最新日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!