Subsonic 3.0 和“Link”桌子

发布于 2024-08-18 21:31:50 字数 2291 浏览 6 评论 0原文

我有以下设置。

博客文章 博客分类 类别

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

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

我已经尝试过这个,但似乎无法正确执行(我得到一个 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;

这很奇怪,似乎连接是错误的,因为只要链接表(将类别链接到博客的表)中存在一些数据,它就会返回所有博客。

下面

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>();

现在

选择 [dbo].[subtext_Links].[LinkID], [dbo].[subtext_Links].[标题], [dbo].[subtext_Links].[网址], [dbo].[subtext_Links].[Rss], [dbo].[subtext_Links].[活动], [dbo].[subtext_Links].[类别ID], [dbo].[subtext_Links].[BlogId], [dbo].[subtext_Links].[PostID], [dbo].[subtext_Links].[NewWindow], [dbo].[subtext_Links].[Rel], \r\n[dbo].[subtext_Content].[ID], [dbo].[subtext_Content].[标题], [dbo].[subtext_Content].[添加日期], [dbo].[subtext_Content].[PostType], [dbo].[subtext_Content].[作者], [dbo].[subtext_Content].[电子邮件], [dbo].[subtext_Content].[BlogId], [dbo].[subtext_Content].[描述], [dbo].[subtext_Content].[更新日期], [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 内部 JOIN [dbo].[subtext_Content] ON [dbo].[subtext_Links].[BlogId] = [dbo].[subtext_Content].[BlogId]\r\n 在哪里 [dbo].[subtext_Links].[类别ID] = @0"

I have the following set-up.

BlogPosts
BlogToCategory
Category

One blog post can have many categorys and a category can be in many blog posts. (Hence the intermediate table.

How would I go about getting a list of all the blog-posts in one category.

I've tried this but cant seem to get it right (I get a IQueryable -> IEnumerable cast error)

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

}

Ok as below I've tried the following.

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;

Now this is wierd it seems the Join is wrong as whenever there is some data in the Links table (Tablethat links category to blog) it returns ALL blogs.

Also tried the below.

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>();

Generated SQL

SELECT [dbo].[subtext_Links].[LinkID],
[dbo].[subtext_Links].[Title],
[dbo].[subtext_Links].[Url],
[dbo].[subtext_Links].[Rss],
[dbo].[subtext_Links].[Active],
[dbo].[subtext_Links].[CategoryID],
[dbo].[subtext_Links].[BlogId],
[dbo].[subtext_Links].[PostID],
[dbo].[subtext_Links].[NewWindow],
[dbo].[subtext_Links].[Rel],
\r\n[dbo].[subtext_Content].[ID],
[dbo].[subtext_Content].[Title],
[dbo].[subtext_Content].[DateAdded],
[dbo].[subtext_Content].[PostType],
[dbo].[subtext_Content].[Author],
[dbo].[subtext_Content].[Email],
[dbo].[subtext_Content].[BlogId],
[dbo].[subtext_Content].[Description],
[dbo].[subtext_Content].[DateUpdated],
[dbo].[subtext_Content].[Text],
[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
WHERE
[dbo].[subtext_Links].[CategoryID] =
@0"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏雪无痕 2024-08-25 21:31:50

您需要加入 BlotToCategory 和 BlogPost 表:

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;

}

You need to join the BlotToCategory and BlogPost tables:

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;

}
墨落画卷 2024-08-25 21:31:50

我已经尝试过了,但似乎无法正确执行(我收到 IQueryable -> IEnumerable 转换错误)

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

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

I've tried this but cant seem to get it right (I get a IQueryable -> IEnumerable cast error)

What about using the .ToList() method?

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

海的爱人是光 2024-08-25 21:31:50

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

IQueryable<BlogToCategory> BlogToCategories {...}

您想要的是

IQueryable<BlogPost> BlogPosts

property on your Category class.
Create a partial class for the Category, and add the IQueryable property:

    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() 应该可用,您确定已包含包含扩展方法的命名空间吗?

Yes, there is a more elegant way. If you're using the ActiveRecord templates, and the Category and BlogPost tables have a foreign key relationship to the BlogToCategory table then your generated Category and BlogPost classes will each have an IQueryable property representing that relationship:

IQueryable<BlogToCategory> BlogToCategories {...}

What you want is an

IQueryable<BlogPost> BlogPosts

property on your Category class.
Create a partial class for the Category, and add the IQueryable property:

    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;
        }
    }

Now you can just call cat.BlogPosts.ToList() - the ToList() should be available, are you sure have included the namespace containing the extension methods?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文