【发布时间】: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