我有下面的安排。
BlogPosts BlogToCategory范畴
一个博客帖子可以有很多分类,一个类别可以在很多博客文章中。(因此是中间表。
我怎样才能得到所有博客的列表--一个类别的帖子。
我试过了,但似乎做不到(我得到了一个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>();生成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\ndbo.subtext_Content.ID,dbo.subtext_Content.Title,dbo.subtext_Content.DateAddeddbo.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.EntryNamedbo.subtext_Content.DateSyndicated\r\n从dbo.subtext_Links\r\n内部加入dbo.subtext_Content ON dbo.subtext_Links.BlogId = dbo.subtext_Content.BlogId\r\n,其中dbo.subtext_Links.CategoryID = @0"
发布于 2010-01-23 12:44:07
您需要加入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;
}发布于 2010-01-23 13:51:25
我试过了,但似乎做错了(我得到了一个IQueryable -> IEnumerable强制转换错误)
如何使用.ToList()方法?
http://msdn.microsoft.com/en-us/library/bb342261.aspx
https://stackoverflow.com/questions/2123113
复制相似问题