LINQ加入多个表多对多

时间:2016-04-22 07:49:29

标签: linq

我有Article和ArticleTopics表。 ArticleTopics表使用ArticleId信息存储记录TopicId。 当尝试检索与特定TopicId一起存储的所有记录时,结果返回null,即使至少有一条记录。

 public static IEnumerable<ArticleWithVotingInfo> GetArticles(string userId, double time, int Skip, int Take, string sort, int topicId)
{
    //Thread.Sleep(1000);
    if (HttpContext.Current.User.Identity.IsAuthenticated)
        userId = HttpContext.Current.User.Identity.GetUserId<string>();
    ArticlesDataContext dataContext = new ArticlesDataContext();
    var results = from article in dataContext.Articles
                                    .Where(t => t.DateAdded <= DateTime.Now.AddHours(-time))
                  join articleTopics in dataContext.ArticleTopics on article.ArticleId equals articleTopics.ArticleId where articleTopics.TopicId == topicId
                  //where zort.ArticleId == article.ArticleId && zort.TopicId == topicId

                  join h in dataContext.Hosts on article.HostID equals h.HostId
                  join c in dataContext.Categories on article.CategoryId equals c.CategoryId
                  join myvote in dataContext.Votes on article.ArticleId equals myvote.ArticleId into JoinedArticleVote
                  from myvote in JoinedArticleVote.Where(v => v.UserId == userId).DefaultIfEmpty()

0 个答案:

没有答案