我想从此Linq查询获得的是所有广告的列表,其中最近关联的日志与LogType.IsStatus == true
的{{1}}已确认或已续订。要清楚,广告有许多日志,每个日志有一个LogType。到目前为止,我有以下内容,但它在LastOrDefault上给出了一个错误System.NotSupportedException。
LogType.Name
答案 0 :(得分:8)
LastOrDefault()
(参见here)。
您可以通过将order by
子查询中的let
子句更改为order by
descending
然后再使用FirstOrDefault()
来解决此问题。
var adverts = (from a in database.Adverts
let lastLog = (from l in a.Logs
where l.LogType.IsStatus == true
orderby l.Created_at descending
select l).FirstOrDefault()
where (lastLog != null)
&&
(lastLog.LogType.Name == "Confirmed" || lastLog.LogType.Name == "Renewed")
orderby a.Created_at descending
select a).ToList();
答案 1 :(得分:0)
试试这个
var lookingType = new string[]{"Confirmed", "Renewed"};
var result = database.Adverts
.Where(entry
=> entry.Logs.Count() > 0 && entry.Logs.Any(log => log.LogType.IsStatus))
.Select(entry
=> new { data = entry,
lastLog = entry.Logs.OrderByDescending(log => log.Created_at).First()
})
.Where(entry => lookingType.Contains(entry.lastLog.LogType.Name))
.Select(entry => entry.data).ToList();