我有3个名为Posts
,Tags
和PostXTags
的表,PostXTags
是帖子和标签之间的连接器。
我尝试在linq中进行以下查询但是我从未实现过理想的结果。
请帮我写一下这个查询的正确linq:
SELECT * FROM Posts
INNER JOIN PostXTags
ON Posts.PostID = PostXTags.PostID
LEFT JOIN Tags
ON PostXTags.TagID = Tags.TagID
答案 0 :(得分:0)
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
答案 1 :(得分:0)
我想你想实现这个目标:
var query = context.Tags.Where(t => t.Id == id).SelectMany(t => t.Posts);