Subsonic 3.0 和“Link”桌子
我有以下设置。
博客文章 博客分类 类别
一篇博客文章可以有多个类别,一个类别可以包含在许多博客文章中。 (因此是中间表。
我如何获取一个类别中所有博客文章的列表。
我已经尝试过这个,但似乎无法正确执行(我得到一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要加入 BlotToCategory 和 BlogPost 表:
You need to join the BlotToCategory and BlogPost tables:
使用 .ToList() 方法怎么样?
http://msdn.microsoft.com/en-us/library/bb342261。 ASPX
What about using the .ToList() method?
http://msdn.microsoft.com/en-us/library/bb342261.aspx
是的,还有一种更优雅的方式。如果您使用的是 ActiveRecord 模板,并且 Category 和 BlogPost 表与 BlogToCategory 表具有外键关系,则生成的 Category 和 BlogPost 类将各自具有表示该关系的 IQueryable 属性:
您想要的是
property on your Category class.
Create a partial class for the Category, and add the IQueryable property:
现在您可以调用 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:
What you want is an
property on your Category class.
Create a partial class for the Category, and add the IQueryable property:
Now you can just call cat.BlogPosts.ToList() - the ToList() should be available, are you sure have included the namespace containing the extension methods?