2010-01-23 70 views
0

我有以下設置。Subsonic 3.0和「鏈接」表

相關博客文章 BlogToCategory 類別

一個博客帖子可以有很多的categorys和類別可以在許多博客文章。 。(因此,中間表

我將如何去獲得一個類別中的所有博客,文章列表

我已經試過,但不能似乎得到它的權利(我得到的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; 

現在,這是奇怪的似乎加入是錯誤的,因爲每當有李的一些數據nks表(平板電腦鏈接類別到博客)它返回所有博客。

還嘗試了下面。

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

SELECT [DBO]。[subtext_Links]。[鏈路ID], [DBO]。[subtext_Links]。[標題], [DBO]。[subtext_Links]。 [dbo], [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 JOIN [dbo]。[subtext_Content] ON [dbo]。[subtext_Links]。[BlogId] = [dbo]。[subtext_Content]。[BlogId] \ [R \ n WHERE [DBO] [subtext_Links] [類別ID] = @ 0"

+0

正在生成什麼sql – 2010-01-23 14:20:00

+0

@Adam - 用SQL更新主文章。 – LiamB 2010-01-23 14:37:49

+0

@ 0是一個參數,不是一個值。以這種格式讀取sql幾乎是不可能的,但它看起來不像是顯而易見的錯誤。當你運行這個sql時它會給出預期的結果嗎? – 2010-01-23 14:45:11

回答

1

你需要加入BlotToCategory和博客帖子表:。

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; 

} 
+0

謝謝 - 我認爲,有沒有更優雅的解決方案? – LiamB 2010-01-23 13:00:40

+0

其實你可以減少到一個連接,我編輯了我的答案,以簡化查詢 – 2010-01-23 13:42:21

+0

看到我已經做了更新的問題 – LiamB 2010-01-23 13:53:50

0

我已經試過這個,但似乎不能得到它的權利(我得到一個IQueryable - > IEnumerable轉換錯誤)

使用.ToList()方法怎麼樣?

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

+0

這種情況並不存在。當使用LinqToSql時它很容易,但亞音速不存在... – LiamB 2010-01-23 13:56:12

0

是的,有一個更優雅的方式。如果您使用ActiveRecord的模板,類別和博客帖子表有到BlogToCategory表的外鍵關係,那麼你的生成的種類和博客帖子班會各有表示關係一個IQueryable屬性:

IQueryable<BlogToCategory> BlogToCategories {...} 

你什麼想要的是您的Category類別上的

IQueryable<BlogPost> BlogPosts
屬性。 創建類別的分部類,並添加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()應該是有的話,你肯定已經包含了命名空間包含擴展方法?