[英]How to write OrderByDescending and where clause in single LinQ query
我想显示特定DeviceId
的最新UpdateDevice
。 使用OrderByDescending
和Where
编写LinQ查询时,出现错误
无法执行文本选择:CS1061'int'不包含'OrderByDescending'的定义,并且找不到扩展方法'OrderByDescending'接受类型为'int'的第一个参数
资料类型
Id - int32
UpdatedDate- datetime
临Q
from a in Attendances
where a.DeviceId == 1
.OrderByDescending(x=>x.Id)
.Take(1)
select a.UpdatedDate
您需要为谓词即where子句添加括号。
完全使用查询语法或lambda表达式。 以下应该工作:
(from a in Attendances
where a.Deviceid == 1
select a)
.OrderByDescending(x=>x.Id)
.Take(1)
.Select(x=>x.UpdatedDate)
或使用lambda表达式语法:
Attendances.Where(a => a.Deviceid == 1)
.OrderByDescending(x=>x.Id)
.Take(1)
.Select(a => a.UpdatedDate);
边注:
如果要返回单个项目,则可以使用FirstOrDefault()
或First()
,以了解两者的区别:
var latestDate = Attendances.Where(a => a.Deviceid == 1)
.OrderByDescending(x=>x.Id)
.FirstOrDefault()
?.UpdatedDate;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.