开发者

trouble with linq query for inner join and left join

I have 3 tables named Posts, 开发者_运维百科Tags and PostXTags that PostXTags is connector between Posts and Tags.

I tried to make the following query in linq But I've never achieved a ideal result.

Please help me write the correct linq this query:

SELECT * FROM Posts
INNER JOIN PostXTags
ON Posts.PostID = PostXTags.PostID
LEFT JOIN Tags
ON PostXTags.TagID = Tags.TagID


var query = from p in Posts
            join x in PostXTags on p.PostID equals x.PostID
            join t in Tags on x.TagID equals t.TagID into joinedTags
            from t in joinedTags.DefaultIfEmpty()  //left join                
            select new { Posts = p, Tags = t };    //t could be null   


I think you want to achieve this:

var query = context.Tags.Where(t => t.Id == id).SelectMany(t => t.Posts);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜