当前位置:  开发笔记 > 数据库 > 正文

LINQ with Subquery/Group By/Join

如何解决《LINQwithSubquery/GroupBy/Join》经验,为你挑选了2个好方法。

新手到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;

笔记:

'Section'有很多'ContentPages'

部分按"SortOrder"字段排序

ContentPages也按'SortOrder'字段排序

表:第
SECTION_ID ....名称....... SortOrder的
.... 1 .........一.......... 1 ......
. ...... 2 .........两个.......... 3 ......
.... 3 .........三...... .... 2 ......

表:ContentPage
Page_ID ....... Section_ID .......标题.............. SortOrder
.... 11 .......... ...... 1 .......... Page One ............. 1 ......
.... 12 ......... .... 1 ........... Page 2 ............. 3 ......
.... 13 ....... ...... 2 ...........第三页........... 2 ......
...... 16 ....... ...... 2 .......... Page Four ............ 4 ......
.... 17 ....... ...... 2 ........... Page Eight ........... 5 ......
.... 18 ....... ...... 1 ........... Page Ten ............. 6 ......

上面的查询可能是用另一种方式编写的,所以这就是我要做的事情:

我需要返回每个Section中第一个ContentPage的列表(当按ContentPage.SortOrder排序时)

按Section.SortOrder对结果进行排序

在结果中显示Section.Name(在Section_ID上加入?)

上面的sql查询不包含最后2个点,而且更像是"很高兴"...

期望的结果
Page_ID ....... Section_ID ... SectionName ..... Title .............. SortOrder
.... 11 ......... .... 1 ......... One ......... Page One ............. 1 ......
.... 13 ............. 2 .........两个..........第三页........... 2. .....

任何帮助表示赞赏.谢谢!



1> NilObject..:

这是我的第一次尝试:

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;

首先我们在section id上创建一个组,以便我们可以在以后获得最小排序顺序.接下来,我们在section id上加入对页面的新引用,并且SortOrder的过滤是section节中的最小值.注意,对于像Min()调用这样的简单表达式,我更喜欢内联lambda表达式而不是另一个查询.

最后,我们添加一个orderby来订购页面,然后我们返回页面(请注意,如果您愿意,可以将其更改为某些字段).



2> GalacticCowb..:

我想这就是你要找的......

    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
sections = new List
(); 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 contentPages = new List(); 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)); } }

请注意,您提供的示例数据显示第2部分的排序顺序为3,但您的示例结果将其排序顺序列为2.

推荐阅读
帆侮听我悄悄说星星
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有