2009-01-13 50 views
2

新手LINQ,並努力寫出下面的查詢......LINQ與子查詢/分組方式/加入

select 
    f.Section_ID, 
    f.Page_ID, 
    f.SortOrder, 
    f.Type 
from 
(
    select 
    Section_ID, 
    min(SortOrder) as minSortOrder 
    from 
    ContentPages 
    group by 
    Section_ID 
) as x 
inner join 
    ContentPages as f on 
    f.Section_ID = x.Section_ID and 
    f.SortOrder = x.minSortOrder; 

注:

  • '科' 有很多 '內容網頁'
  • 段由「SortOrder的」字段排序
  • 內容網頁,也可以通過一個「SortOrder的」字段排序

表:部分
Section_ID ....名稱....... SortOrder
.... 1 .........一個.......... 1。 .....
.... 2 ......... Two .......... 3 ......
.... 3 .... ..... 3 ........ 2 ......

Table:ContentPage
Page_ID ....... Section_ID ....... Title ... ........... SortOrder
.... 11 ............. 1 ..........第一頁..... ........ 1 ......
.... 12 ............. 1 ...........第二頁。 ............ 3 ......
.... 13 ............. 2 ...........第3頁........... 2 .....
.... 16 ............. 2 ..........第四頁............ 4 ... ...
.... 17 ............. 2 ...........第8頁........... 5。 .....
.... 18 ............. 1 ...........第十頁........... ..6 ......

上面的查詢也可能會被寫成另一種方式,所以這裏就是我想要做的事:

  • 我需要返回內第一ContentPage名單每個部分(當按ContentPage.SortOrder排序時)
  • 通過Section.SortOrder
  • 顯示Section.Name 210級
  • 排序(加入上SECTION_ID?)結果以及

最後2分並不受上面的SQL查詢和更多的「好有」 ......

所需的結果
PAGE_ID ....... SECTION_ID ... SectionName .....標題.............. SortOrder的
.... 11 ............. 1 .........一個.........第一頁.......... ... 1 ......
.... 13 ............. 2 ......... 2 ......... .Page三........... 2 ......

任何幫助表示讚賞。謝謝!

回答

8

這是我第一次嘗試吧:

from sectionPage in pages 
group sectionPage by sectionPage.Section_ID into sectionGroup 
join page in pages on sectionGroup.Key equals page.Section_ID 
where page.SortOrder == sectionGroup.Min(p => p.SortOrder) 
orderby page.SortOrder 
select page; 

所發生的是第一,我們對部分ID創建一個組,以便我們可以在以後得到最小的排序順序。接下來,我們加入對部分ID的頁面的新參考,並按部分組中的最小值對SortOrder進行過濾。請注意,對於Min()調用等簡單表達式,我更喜歡將內聯lambda表達式置於另一個查詢中。

最後,我們添加一個orderby來訂購頁面,並返回頁面(如果您願意,可以將其更改爲某些字段)。

5

我認爲這是你在找什麼...

internal class Section 
    { 
     public int SectionId { get; set; } 
     public string Name { get; set; } 
     public int SortOrder { get; set; } 
    } 

    internal class ContentPage 
    { 
     public int PageId { get; set; } 
     public int SectionId { get; set; } 
     public string Title { get; set; } 
     public int SortOrder { get; set; } 
    } 

    static void Main(string[] args) 
    { 
     List<Section> sections = new List<Section>(); 
     sections.Add(new Section() { SectionId = 1, Name = "One", SortOrder = 1 }); 
     sections.Add(new Section() { SectionId = 2, Name = "Two", SortOrder = 3 }); 
     sections.Add(new Section() { SectionId = 3, Name = "Three", SortOrder = 2 }); 

     List<ContentPage> contentPages = new List<ContentPage>(); 
     contentPages.Add(new ContentPage() { PageId = 11, SectionId = 1, Title = "Page One", SortOrder = 1 }); 
     contentPages.Add(new ContentPage() { PageId = 12, SectionId = 1, Title = "Page Two", SortOrder = 3 }); 
     contentPages.Add(new ContentPage() { PageId = 13, SectionId = 2, Title = "Page Three", SortOrder = 2 }); 
     contentPages.Add(new ContentPage() { PageId = 16, SectionId = 2, Title = "Page Four", SortOrder = 4 }); 
     contentPages.Add(new ContentPage() { PageId = 17, SectionId = 2, Title = "Page Eight", SortOrder = 5 }); 
     contentPages.Add(new ContentPage() { PageId = 18, SectionId = 1, Title = "Page Ten", SortOrder = 6 }); 

     var items = from section in sections 
        orderby section.SortOrder 
        join contentPage in 
         (from contentPage in contentPages 
          orderby contentPage.SortOrder 
          group contentPage by contentPage.SectionId into grp 
          select grp.FirstOrDefault()) 
        on section.SectionId equals contentPage.SectionId 
        select new 
        { 
         PageId = contentPage.PageId, 
         SectionId = section.SectionId, 
         SectionName = section.Name, 
         Title = contentPage.Title, 
         SortOrder = section.SortOrder 
        }; 

     foreach (var newItem in items) 
     { 
      Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}", newItem.PageId, newItem.SectionId, newItem.SectionName, newItem.Title, newItem.SortOrder)); 
     } 
    } 

請注意,您提供的樣本數據顯示的3款2排序順序,但你的樣品結果列出它的排序順序2.