How can I get distinct records from many to many relationship using Linq to Sql
For brevity, let's say I have the following 3 tables (m:n): Articles, Topics and a joining table ArticleTopic. I need to get the top n articles for n topics. Each article should only show once for the entire result set.
Article: -Id -Title
Topic: -Id -Name
ArticleTopic: -Id -ArticleId -TopicId
Thanks for your 开发者_Python百科help!
topics
.SelectMany(topic => topic.ArticleTopic)
.Select(articleTopic => article)
.Distinct()
But it is not obvious to me what you mean by top n - I can not see any ranking information in your question.
精彩评论