Entity framework 连接两个表并从第一个表返回对象的Linq查询-使用PagedList
我使用实体框架(代码优先模型) 我有表Entity framework 连接两个表并从第一个表返回对象的Linq查询-使用PagedList,entity-framework,linq,lambda,pagedlist,Entity Framework,Linq,Lambda,Pagedlist,我使用实体框架(代码优先模型) 我有表帖子(postID | Title |…)和表评论(commentID | Comment | postID | userID | CommentDate |…) 我想要一个基于评论中用户ID标准的帖子列表 var listOfPosts = (from p in db.Posts join coment in db.Comments on p.postID equals com
帖子(postID | Title |…)
和表评论(commentID | Comment | postID | userID | CommentDate |…)
我想要一个基于评论中用户ID标准的帖子列表
var listOfPosts = (from p in db.Posts
join coment in db.Comments
on p.postID equals coment.postID
where coment.userID == "Some value"
orderby coment.CommentDate descending
select p).ToList();
return View("ReportList", listOfReports.ToPagedList(pageNumber, pageSize));
我还使用PagedList对文章进行分页
但是我希望结果按PostID分组,所以我修改了代码如下
var listOfPosts = (from p in db.Posts
join coment in db.Comments
on p.postID equals coment.postID
where coment.userID == "Some value"
orderby coment.CommentDate descending
group p by p.postID into newP
select newP).ToList();
这里的问题是,第二次查询的结果返回List
和ToPagedList
页面列表的方法仅适用于List
如何更改查询以根据条件和评论顺序返回不同的帖子列表?
您可以使用lambda表达式或查询语法。您可以尝试在外部查询上使用subquery和distinct,如下所示:
var listOfPosts = (from b in ((from p in db.Posts
join coment in db.Comments on p.postID equals coment.postID
where coment.userID == "Some value"
orderby coment.ComentDate descending
select p).ToList())
select b).Distinct().ToList();