2009-01-13 16 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.