I want to show the latest UpdateDevice
-value of a particular DeviceId
. When writing a LinQ query using OrderByDescending
and Where
, I'm getting the error
Cannot execute text selection: CS1061 'int' does not contain a definition for 'OrderByDescending' and no extension method 'OrderByDescending' accepting a first argument of type 'int' could be found
Datatypes
Id - int32
UpdatedDate- datetime
LinQ
from a in Attendances
where a.DeviceId == 1
.OrderByDescending(x=>x.Id)
.Take(1)
select a.UpdatedDate
You need to put parenthesis for you predicate ie where clause.
Either use query syntax or lambda expression completely. The following should work:
(from a in Attendances
where a.Deviceid == 1
select a)
.OrderByDescending(x=>x.Id)
.Take(1)
.Select(x=>x.UpdatedDate)
or use lambda expression syntax:
Attendances.Where(a => a.Deviceid == 1)
.OrderByDescending(x=>x.Id)
.Take(1)
.Select(a => a.UpdatedDate);
Side Note:
If single item is intended to be returned, then you can use FirstOrDefault()
or First()
, you can read about the difference of both :
var latestDate = Attendances.Where(a => a.Deviceid == 1)
.OrderByDescending(x=>x.Id)
.FirstOrDefault()
?.UpdatedDate;
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.