C# LINQ到实体查询生成奇数错误
我有一个论坛,我正在列出最近活跃的话题。我会在最后一个回复日期之前对主题进行排序,如果某个主题没有回复,则是该主题的发布日期。以下查询工作正常:C# LINQ到实体查询生成奇数错误,c#,linq,entity-framework,linq-to-entities,C#,Linq,Entity Framework,Linq To Entities,我有一个论坛,我正在列出最近活跃的话题。我会在最后一个回复日期之前对主题进行排序,如果某个主题没有回复,则是该主题的发布日期。以下查询工作正常: var topicsQuery = from x in board.Topics let lastActivityDate = x.Replies.Any() ? x.Replies.OrderBy(y => y.
var topicsQuery = from x in board.Topics
let lastActivityDate = x.Replies.Any()
? x.Replies.OrderBy(y => y.PostedDate).Last().PostedDate
: x.PostedDate
orderby lastActivityDate descending
select x;
这个查询非常有效。每次页面加载时,主题的顺序都是正确的。但是,现在我有一个ajax调用,它查找更新的活动并运行类似的查询:
topics = (from x in DBContext.Topics
let lastActivityDate = (x.Replies.Any()
? x.Replies.OrderBy(y => y.PostedDate).Last().PostedDate
: x.PostedDate)
where x.BoardID == boardID
where lastActivityDate > lastTopic.PostedDate
orderby lastActivityDate
select x).ToList<Topic>();
topics=(来自DBContext.topics中的x)
让lastActivityDate=(x.replays.Any()
?x.repress.OrderBy(y=>y.PostedDate).Last().PostedDate
:x.postedate)
其中x.BoardID==BoardID
其中lastActivityDate>lastTopic.PostedDate
orderby lastActivityDate
选择x).ToList();
有人看到这个LINQ查询有什么问题吗?它正在生成以下错误:
LINQ to Entities无法识别方法“MyProject.Models.Reply Last[Reply](System.Collections.Generic.IEnumerable`1[MyProject.Models.Reply]),并且无法将此方法转换为存储表达式。失败的原因是因为尚未加载实体和Last()将对sql而不是泛型列表中的sql调用。因此,首先需要在请求Last()之前加载它们。第一个示例可能有效,因为板已经加载了通用列表 请尝试以下操作:
topics = (from x in DBContext.Topics.AsEnumerable<Topic>()
let lastActivityDate = (x.Replies.Any()
? x.Replies.OrderBy(y => y.PostedDate).Last().PostedDate
: x.PostedDate)
where x.BoardID == boardID
where lastActivityDate > lastTopic.PostedDate
orderby lastActivityDate
select x).ToList<Topic>();
topics=(来自DBContext.topics.AsEnumerable()中的x)
让lastActivityDate=(x.replays.Any()
?x.repress.OrderBy(y=>y.PostedDate).Last().PostedDate
:x.postedate)
其中x.BoardID==BoardID
其中lastActivityDate>lastTopic.PostedDate
orderby lastActivityDate
选择x).ToList();
请参阅:失败的原因是尚未加载实体,并且将对不在常规列表中的sql调用Last()。因此,首先需要在请求Last()之前加载它们。第一个示例可能有效,因为板已经加载了通用列表 请尝试以下操作:
topics = (from x in DBContext.Topics.AsEnumerable<Topic>()
let lastActivityDate = (x.Replies.Any()
? x.Replies.OrderBy(y => y.PostedDate).Last().PostedDate
: x.PostedDate)
where x.BoardID == boardID
where lastActivityDate > lastTopic.PostedDate
orderby lastActivityDate
select x).ToList<Topic>();
topics=(来自DBContext.topics.AsEnumerable()中的x)
让lastActivityDate=(x.replays.Any()
?x.repress.OrderBy(y=>y.PostedDate).Last().PostedDate
:x.postedate)
其中x.BoardID==BoardID
其中lastActivityDate>lastTopic.PostedDate
orderby lastActivityDate
选择x).ToList();
请参阅:我看不出与OP的版本有什么不同,你能强调一下区别吗?@Jani:用更多的上下文更新了答案。@Mahesh这可能会伤害dude,你将所有主题都加载到内存中,这是一个非常昂贵的DBMS和应用服务器解决方案,这不是一个好办法idea@Jani:同意,试图说明为什么它不起作用,而不是给出一个最佳的方式。但我不认为我们现在有更好的性能,获取所有帖子的sql也会运行,只是查询有不同的执行,而在以前的代码示例中,我们有急切的执行。我看不出与OP的版本有什么区别,你能强调一下区别吗?@Jani:用更多的上下文更新了答案。@Mahesh这可能会伤害dude,你将所有主题都加载到内存中,这是一个非常昂贵的DBMS和应用服务器解决方案,这不是一个好办法idea@Jani:同意,试图说明为什么它不起作用,但是我不认为我们现在有更好的性能,相同的sql可以运行,只不过查询的执行不同,在前面的代码示例中,我们有急切的执行。