我需要帮助将此SQL语句转换为EF4:
Select Posts.PostID, Post, Comment
from Posts left join
Comments on posts.PostID = Comments.PostID
Where CommentID not in
(
Select PostID
from Votes
where VoteTypeID = 4 --4 = flagged comment type
)在我的数据库中,Votes表在Votes.PostID列中存储已报告帖子的PostID或已报告评论的CommentID
提前感谢!
发布于 2010-12-23 13:51:02
using (var context = new Model1Container())
{
var posts = context.Posts.
All((p)=> p.Votes.All((v) => v.VoteTypeId!=4));
//Or
var posts2 = from p in context.Posts
where p.Votes.All((v)=> v.VoteTypeId != 4)
select p;
}更新:
根据我的理解,你想要所有的帖子,但是对于每个帖子,你想过滤它的评论,如果是这样的话,你可以使用ToDictionary
var posts =
context.Posts.
//Include("Comments").
ToDictionary(
(p) => p,
(p) => p.Comments.Where((c)=> c.Votes.All((v) => v.VoteTypeId !=4))
);
foreach (var item in posts)
{
var post = item.Key;
var comments = item.Value;
}注意:如果延迟加载被禁用,并且您明确希望立即加载此查询中的注释,则取消对Include方法的注释。
Update2:
var postsCollection = posts.Keys.ToArray();
var commentsCollection = posts.Values.ToArray();发布于 2010-12-23 13:30:18
如果不看你的模型,很难确定这一点。但这应该是你开始学习的起点。如果你愿意,你可以上传一张你的模型或EDMX的照片,我可以更好地看看这个。
var myPosts = from p in posts
where !p.Comments.Any(c => c.Votes.VoteID != 4)
PostId = p.PostId,
//other field needed here
};https://stackoverflow.com/questions/4516033
复制相似问题