2016-05-24 5 views
1

を使用私は、エンティティフレームワーク(コードもみモデル) を使用し、私はテーブルPosts(postID | Title | ...)とテーブルComments(commentID | Comment | postID | userID | CommentDate | ...)LINQクエリ - PagedListは

を持っている私は、の基準に基づいて記事のリストが欲しいですコメントのuserID。

また、私はポケットのページ設定にPagedListを使用します。

しかし、私は

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(); 

ここでの問題は、2番目のクエリの結果はList<IGrouping<int, Posts>>を返し、PagedListのToPagedList方法はList<Posts>

でのみ動作していることであるが、以下のように私は、コードを変更した結果がPostIDでグループになりたいです

基準とコメントの順序に基づいて投稿の別のリストを返すようにクエリを変更するにはどうすればよいですか? ラムダ式またはクエリ構文を使用できます。

答えて

1

あなたは以下のような外部クエリでサブクエリと個別のを使用しようとすることができます:

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();