【问题标题】:Subsonic 3.0 and "Link" tables亚音速 3.0 和“链接”表
【发布时间】:2011-01-08 13:11:24
【问题描述】:

我有以下设置。

博文 博客分类 类别

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

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

我已经尝试过了,但似乎无法正确(我收到 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 表(将类别链接到博客的表)中有一些数据,它就会返回所有博客。

还尝试了以下。

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

选择 [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].[作者], [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 加入 [dbo].[subtext_Content] ON [dbo].[subtext_Links].[BlogId] = [dbo].[subtext_Content].[BlogId]\r\n 在哪里 [dbo].[subtext_Links].[CategoryID] = @0"

【问题讨论】:

  • 正在生成什么sql
  • @Adam - 使用 SQL 更新主帖。
  • @0 是参数而不是值。以这种格式读取 sql 几乎是不可能的,但看起来它显然不是错误的。当您运行该 sql 时,它会给出预期的结果吗?
  • 是的,我发帖后就知道了。我是一个工具。谢谢

标签: c# asp.net linq subsonic subsonic3


【解决方案1】:

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

IQueryable<BlogToCategory> BlogToCategories {...}

你想要的是一个

IQueryable BlogPosts
Category 类的属性。 为 Category 创建一个部分类,并添加 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() 应该可用,您确定已包含包含扩展方法的命名空间吗?

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 这种情况下不存在。当使用 LinqToSql 时,它很容易,但亚音速它不存在......
    【解决方案3】:

    您需要加入 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;
    
    }
    

    【讨论】:

    • 谢谢 - 我想,有没有更优雅的解决方案?
    • 其实你可以把它减少到一个连接,我已经编辑了我的答案来简化查询
    • 我已经解决了。术语 BlogId 和 PostId 之间的混淆。
    • 更优雅的解决方案是创建一个视图,永远不明白为什么当您可以运行通用 sql 语句并从中构建视图时人们会创建复杂的代码,subsonic 然后创建一个类,就像它做表然后你只需 ViewAllBlog.All().Where(x => x.CategoryID==catid);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多