亚音速3.0和“链接”表

时间:2010-01-23 12:24:48

标签: c# asp.net linq subsonic subsonic3

我有以下设置。

的相关博客文章 BlogToCategory 分类

一篇博文可以有很多类别,一个类别可以在很多博客文章中。 (因此是中间表。

我如何获取一个类别中所有博客帖子的列表。

我已经尝试了这个,但似乎无法正确使用(我得到了一个IQueryable - > IEnumerable强制转换错误)

public IEnumerable<BlogPost> FetchAllBlogs(int? CatId)
{
        return from c in CategoryLink.All()
                   where c.CategoryID == CatId
                   select c.BlogPost;

}

好的如下我尝试了以下内容。

return from blogToCategories in subtext_Link.All()
                      join blogPosts in subtext_Content.All() on blogToCategories.BlogId equals blogPosts.BlogId
                      where blogToCategories.CategoryID == CatId
                      orderby (blogPosts.DateAdded) descending
                      select blogPosts;

现在这很奇怪看起来Join是错误的,因为只要Links表中有一些数据(Tablethat将类别链接到博客),它就会返回所有博客。

还试过以下。

BlogList = new TransformDB().Select
                  .From<subtext_Content>()
                  .InnerJoin<subtext_Link>(subtext_LinksTable.BlogIdColumn, subtext_ContentTable.BlogIdColumn)
                  .Where(subtext_LinksTable.CategoryIDColumn).IsEqualTo(CatId)
                  .ExecuteTypedList<subtext_Content>();

生成SQL

  

SELECT [dbo]。[subtext_Links]。[LinkID],   [DBO]。[subtext_Links]。[标题]   [DBO]。[subtext_Links]。[URL],   [DBO]。[subtext_Links] [RSS]   [DBO]。[subtext_Links]。[有效],   [DBO]。[subtext_Links] [类别ID],   [DBO]。[subtext_Links] [BlogId]   [DBO]。[subtext_Links] [帖子ID],   [DBO]。[subtext_Links] [NewWindow]   [DBO]。[subtext_Links] [相对]   \ r \ n [DBO]。[subtext_Content]。[ID],   [DBO]。[subtext_Content]。[标题]   [DBO]。[subtext_Content] [DateAdded]   [DBO]。[subtext_Content] [PostType]   [DBO]。[subtext_Content]。[作者]   [DBO]。[subtext_Content] [电子邮件]   [DBO]。[subtext_Content] [BlogId]   [DBO]。[subtext_Content] [描述],   [DBO]。[subtext_Content] [DateUpdated]   [DBO]。[subtext_Content]。[文字],   [DBO]。[subtext_Content] [FeedBackCount]   [DBO]。[subtext_Content] [PostConfig]   [DBO]。[subtext_Content] [EntryName]   [DBO]。[subtext_Content] [DateSyndicated] \ r \ n   FROM [dbo]。[subtext_Links] \ r \ n INNER   JOIN [dbo]。[subtext_Content] ON   [dbo]。[subtext_Links]。[BlogId] =   [DBO]。[subtext_Content] [BlogId] \ r \ n   哪里   [dbo]。[subtext_Links]。[CategoryID] =   @ 0"

3 个答案:

答案 0 :(得分:1)

您需要加入BlotTo类别和博客帖子表:

public IEnumerable<BlogPost> FetchAllBlogs(int? CatId)
{
  return from blogToCategories in BlogToCategory.All() 
         join blogPosts in BlogPost.All() on blogPosts.Id equals blogToCategories.BlogId 
         where blogToCategories.CategoryID == CatId
         select blogPosts;

}

答案 1 :(得分:0)

  

我已经尝试了这个,但似乎无法正确使用(我得到了一个IQueryable - &gt; IEnumerable强制转换错误)

使用.ToList()方法怎么样?

http://msdn.microsoft.com/en-us/library/bb342261.aspx

答案 2 :(得分:0)

是的,有一种更优雅的方式。如果您正在使用ActiveRecord模板,并且Category和BlogPost表与BlogToCategory表具有外键关系,那么您生成的Category和BlogPost类将各自具有表示该关系的IQueryable属性:

IQueryable<BlogToCategory> BlogToCategories {...}

你想要的是你的Category类的

IQueryable<BlogPost> BlogPosts
属性。 为Category创建一个partial类,并添加IQueryable属性:

    public IQueryable<BlogPost> BlogPosts
    {
        get
        {
            var repo = BlogPost.GetRepo();
            return from items in repo.GetAll()
                   join linkItems in BlogToCategories 
                   on items.CatID equals linkItems.CategoryID
                   select items;
        }
    }

现在你可以调用cat.BlogPosts.ToList() - ToList()应该可用,你确定包含了包含扩展方法的命名空间吗?