简体   繁体   中英

Where clause to get last record created

Using LINQ would like to use a where clause to get last record created in table.

Not sure of exact syntax:

POSUnitRecord lastDownloadedPOS = lastDownloadedPOS .Where...

I'd go with

lastDownloadedPOS.OrderByDescending(x => x.?????).FirstOrDefault();

Where ???? would be the name of the property holding the date you're after

使用以下查询:

  POSUnitRecord lastDownloadedPOS = lastDownloadedPOS .Where(p=>p.columnName==value).OrderByDescending(p=>p.columnName).FirstOrDefault();

The last record on the database whould probably have the newest timestamp.

Try to use the max function of linq instead of the where to retrieve one record containing the timestamp, another query could then be done to retrieve the last record.

DateTime lastRecord = lastDownloadedPos.Max(a=>a.Timestamp);
lastDownloadedPos.SingleOrDefault(a=>a.Timestamp == lastRecord);

根据您的收藏类型,您可以执行以下操作:

POSUnitRecord lastDownloadedPOS = lastDownloadedPOSCollection.Last();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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