LINQ与设子查询 [英] Linq with subquery with Let
问题描述
需要用LINQ查询一些帮助。我试图从查询得到的是所有的广告获取最新的相关日志与LogType.IsStatus ==真有LogType.Name的确认或续期的列表。需要明确的是,一个广告有许多日志和日志每有一个LOGTYPE。到目前为止,我有以下的,但它给我的LastOrDefault错误System.NotSupportedException。任何想法?
VAR广告=(来自于database.Adverts
让lastlog的=(从升的a.Logs
其中,l.LogType.IsStatus == TRUE
排序依据l.Created_at
选择L).LastOrDefault()
其中(lastlog的!= NULL)
&功放;&安培;
(lastLog.LogType.Name ==确认|| lastLog.LogType.Name ==续期)
排序依据a.Created_at降
选择一个).ToList();
由于提前,
周杰伦
编辑:
新版本中,如果任何人有在今后类似的问题。的解决办法是首先编译的所有项目的列表,则子查询中的两个步骤,而不是在一气呵成列表
VAR广告=(从在database.Adverts.ToList()
让lastStatusLog = a.Logs.Where(L => l.LogType.IsStatus ==真).OrderByDescending(L = GT; l.Created_at).FirstOrDefault()
其中,lastStatusLog!= NULL
&功放;&安培;
(lastStatusLog.LogType.Name ==证实|| lastStatusLog.LogType.Name ==重申)
排序依据a.Created_at下降
选择一个).ToList();
LastOrDefault()
在LINQ是不支持实体(见的这里)。
您可以变通的作法是由条款变更为了您的
让
子查询到按
降序
,然后用 FirstOrDefault()
代替。
VAR广告=(来自于database.Adverts
让lastlog的=(从升的a.Logs
其中,l.LogType.IsStatus == TRUE
排序依据l.Created_at降
选择L).FirstOrDefault()
其中(lastlog的!= NULL)
&功放;&安培;
(lastLog.LogType.Name ==确认|| lastLog.LogType.Name ==续期)
排序依据a.Created_at降
选择一个).ToList();
Need some assistance with a Linq query. What I'm trying to get from the 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 its giving me an error System.NotSupportedException on the LastOrDefault. Any Ideas?
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();
Thanks in advance,
Jay
EDIT: New version, in case anyone has a similar problem in the future. Solution was to first compile a list of all of the items, then subquery the list in two steps rather than in one go.
var adverts = (from a in database.Adverts.ToList()
let lastStatusLog = a.Logs.Where(l => l.LogType.IsStatus == true).OrderByDescending(l => l.Created_at).FirstOrDefault()
where lastStatusLog != null
&&
(lastStatusLog.LogType.Name == "Confirmed" || lastStatusLog.LogType.Name == "Renewed")
orderby a.Created_at descending
select a).ToList();
LastOrDefault()
is not supported in LINQ to Entities (see here).
You can work around this by changing the order by
clause in your let
subquery to an order by
descending
and then use FirstOrDefault()
instead.
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();
这篇关于LINQ与设子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!