How to get the latest date inserted from the table with Linq to entities

馋奶兔 提交于 2019-12-10 13:47:14

问题


how to retrieve the latestdate inspected from a column

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

Let say table name is Statuses and col name is LastdateInspected


回答1:


Sounds like you want something like:

var query = db.Statuses
              .OrderByDescending(x => x.LastDateInspected)
              .FirstOrDefault();

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

That will get you the whole record, of course. If you only want the date, you can select the LastDateInspected column.



来源:https://stackoverflow.com/questions/9525470/how-to-get-the-latest-date-inserted-from-the-table-with-linq-to-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!