使用 Let 进行子查询的 Linq
我试图从这个 Linq 查询中获取的是所有广告的列表,其中最近关联的具有 LogType.IsStatus == true
的日志具有 LogType.Name
已确认或已更新。需要明确的是,一个 Advert 有很多 Log,每个 Log 有一个 LogType。到目前为止,我有以下内容,但它在 LastOrDefault 上给了我一个错误 System.NotSupportedException。
var adverts = (from a in database.Adverts
let lastLog = (from l in a.Logs
where l.LogType.IsStatus == true
orderby l.Created_at
select l).LastOrDefault()
where (lastLog != null)
&&
(lastLog.LogType.Name == "Confirmed" || lastLog.LogType.Name == "Renewed")
orderby a.Created_at descending
select a).ToList();
What I'm trying to get from this Linq query is a list of all Adverts where the most recent associated Log with a LogType.IsStatus == true
has a LogType.Name
of either Confirmed or Renewed. To be clear, an Advert has many Logs and each Log has one LogType. So far I have the following, but it's giving me an error System.NotSupportedException on the LastOrDefault.
var adverts = (from a in database.Adverts
let lastLog = (from l in a.Logs
where l.LogType.IsStatus == true
orderby l.Created_at
select l).LastOrDefault()
where (lastLog != null)
&&
(lastLog.LogType.Name == "Confirmed" || lastLog.LogType.Name == "Renewed")
orderby a.Created_at descending
select a).ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LINQ to Entities 不支持
LastOrDefault()
(请参阅您可以通过将
let
子查询中的order by
子句更改为order by
descending
来解决此问题,然后请改用 FirstOrDefault() 。LastOrDefault()
is not supported in LINQ to Entities (see here).You can work around this by changing the
order by
clause in yourlet
subquery to anorder by
descending
and then useFirstOrDefault()
instead.试试这个
try this