简体   繁体   English

Subsonic 3.0和“链接”表

[英]Subsonic 3.0 and “Link” tables

I have the following set-up. 我有以下设置。

BlogPosts BlogToCategory Category BlogPosts BlogToCategory类别

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) 我已经尝试过了,但是似乎无法正确处理(我收到了IQueryable-> IEnumerable强制转换错误)

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. 现在,这似乎有点奇怪,因为“链接”表(将类别链接到博客的表)中存在某些数据时,Join似乎是错误的,它将返回所有博客。

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 生成的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_Link 选择[dbo]。[subtext_Links]。[LinkID],[dbo]。[subtext_Links]。[Title],[dbo]。[subtext_Links]。[Url],[dbo]。[subtext_Links]。[Rss],[ dbo]。[subtext_Links]。[活动],[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]。[标题],[ 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内部联接[dbo]。[subtext_Content]打开[dbo]。[subtext_Links]。[BlogId] = [dbo]。[subtext_Content]。[BlogId] \\ r \\ n [dbo]。 [subtext_Link s].[CategoryID] = @0" s]。[CategoryID] = @ 0“

You need to join the BlotToCategory and BlogPost tables: 您需要加入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;

}

I've tried this but cant seem to get it right (I get a IQueryable -> IEnumerable cast error) 我已经尝试过了,但是似乎无法正确处理(我收到了IQueryable-> IEnumerable强制转换错误)

What about using the .ToList() method? 使用.ToList()方法怎么样?

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

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: 如果您使用的是ActiveRecord模板,并且Category和BlogPost表与BlogToCategory表具有外键关系,则生成的Category和BlogPost类将分别具有表示该关系的IQueryable属性:

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

Now you can just call cat.BlogPosts.ToList() - the ToList() should be available, are you sure have included the namespace containing the extension methods? 现在您可以只调用cat.BlogPosts.ToList()-ToList()应该可用,您确定已包含包含扩展方法的名称空间吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM