0

我正在尝试使用 linq to sql 执行以下 SQL 查询。

select  COURSE_SECTIONS.SEC_SUBJECT,
        COURSE_SECTIONS.SEC_COURSE_NO,
        COURSE_SECTIONS.SEC_NO,
        SectionEnrollments.SectionEnrollment
from    COURSE_SECTIONS
        inner join  (select STC_COURSE_NAME, STC_SECTION_NO, COUNT(*) AS SectionEnrollment
        from        STUDENT_ACAD_CRED
        where       STC_TERM = '2012FL'
        group       by STC_COURSE_NAME, STC_SECTION_NO) as SectionEnrollments
                    on COURSE_SECTIONS.SEC_SUBJECT + '-' + COURSE_SECTIONS.SEC_COURSE_NO = SectionEnrollments.STC_COURSE_NAME
                       and COURSE_SECTIONS.SEC_NO = SectionEnrollments.STC_SECTION_NO
where   SEC_TERM = '2012FL'

我的第一次尝试是这个

var query = from sections in db.COURSE_SECTIONS
    where sections.SEC_TERM == "2012FL"
    orderby sections.SEC_SUBJECT, sections.SEC_COURSE_NO, sections.SEC_NO
    select 
        new 
        { 
            sections.SEC_SUBJECT, 
            sections.SEC_COURSE_NO, 
            sections.SEC_NO,
            sectionEnrollmentCount = (from enrollments in db.STUDENT_ACAD_CRED
                where enrollments.STC_TERM == "2012FL"
                group enrollments by
                    new
                    {
                        STC_COURSE_NAME = enrollments.STC_COURSE_NAME,
                        STC_SECTION_NO = enrollments.STC_SECTION_NO
                    } into grouping
                where grouping.Key.STC_COURSE_NAME == sections.SEC_SUBJECT + "-" + sections.SEC_COURSE_NO
                    && grouping.Key.STC_SECTION_NO == sections.SEC_NO
                select grouping.Count()).FirstOrDefault()
        };

这实际可行,但需要将近 30 来返回结果(大约 900 条记录)。SQL 需要不到一秒钟的时间。

这是我下一次尝试让它运行得更快的尝试。

var query2 = from sections in db.COURSE_SECTIONS
    join groupingQuery in 
        (from enrollments in db.STUDENT_ACAD_CRED
        where enrollments.STC_TERM == "2012FL"
        group enrollments by
            new
            {
                STC_COURSE_NAME = enrollments.STC_COURSE_NAME,
                STC_SECTION_NO = enrollments.STC_SECTION_NO
            } into grouping
        select new { grouping.Key, EnrollmentCount = grouping.Count() } )
        on sections.SEC_SUBJECT + "-" + sections.SEC_COURSE_NO + "-" + sections.SEC_NO equals groupingQuery.Key.STC_COURSE_NAME + "-" + groupingQuery.Key.STC_SECTION_NO
        into sectionEnrollmentCount
    where sections.SEC_TERM == "2012FL"
    orderby sections.SEC_SUBJECT, sections.SEC_COURSE_NO, sections.SEC_NO
    select
        new
        {
            sections.SEC_SUBJECT,
            sections.SEC_COURSE_NO,
            sections.SEC_NO,
        sectionEnrollmentCount
        };

但是,对于 sectionEnrollmentCount 变量类型是 System.Collections.Generic.List 1[<>f__AnonymousType22[<>f__AnonymousType0`2[System.String,System.String],System.Int32]] 我不知道如何让它成为一个号码。

有人可以帮我解决这个问题吗?

谢谢,

编辑

这是第一次尝试生成的 SQL。

SELECT 
[Project1].[C1] AS [C1], 
[Project1].[SEC_SUBJECT] AS [SEC_SUBJECT], 
[Project1].[SEC_COURSE_NO] AS [SEC_COURSE_NO], 
[Project1].[SEC_NO] AS [SEC_NO], 
[Project1].[C2] AS [C2]
FROM ( SELECT 
[Filter1].[SEC_SUBJECT] AS [SEC_SUBJECT], 
[Filter1].[SEC_COURSE_NO] AS [SEC_COURSE_NO], 
[Filter1].[SEC_NO] AS [SEC_NO], 
1 AS [C1], 
CASE WHEN ([Limit1].[A1] IS NULL) THEN 0 ELSE [Limit2].[A1] END AS [C2]
FROM    (SELECT [Extent1].[SEC_SUBJECT] AS [SEC_SUBJECT], [Extent1].[SEC_COURSE_NO] AS [SEC_COURSE_NO], [Extent1].[SEC_NO] AS [SEC_NO]
    FROM [dbo].[COURSE_SECTIONS] AS [Extent1]
    WHERE '2012FL' = [Extent1].[SEC_TERM] ) AS [Filter1]
OUTER APPLY  (SELECT TOP (1) [GroupBy1].[A1] AS [A1]
    FROM ( SELECT 
        [Extent2].[STC_COURSE_NAME] AS [K1], 
        [Extent2].[STC_SECTION_NO] AS [K2], 
        COUNT(1) AS [A1]
        FROM [dbo].[STUDENT_ACAD_CRED] AS [Extent2]
        WHERE '2012FL' = [Extent2].[STC_TERM]
        GROUP BY [Extent2].[STC_COURSE_NAME], [Extent2].[STC_SECTION_NO]
    )  AS [GroupBy1]
    WHERE ([GroupBy1].[K1] = ([Filter1].[SEC_SUBJECT] + N'-' + [Filter1].[SEC_COURSE_NO])) AND ([GroupBy1].[K2] = [Filter1].[SEC_NO]) ) AS [Limit1]
OUTER APPLY  (SELECT TOP (1) [GroupBy2].[A1] AS [A1]
    FROM ( SELECT 
        [Extent3].[STC_COURSE_NAME] AS [K1], 
        [Extent3].[STC_SECTION_NO] AS [K2], 
        COUNT(1) AS [A1]
        FROM [dbo].[STUDENT_ACAD_CRED] AS [Extent3]
        WHERE '2012FL' = [Extent3].[STC_TERM]
        GROUP BY [Extent3].[STC_COURSE_NAME], [Extent3].[STC_SECTION_NO]
    )  AS [GroupBy2]
    WHERE ([GroupBy2].[K1] = ([Filter1].[SEC_SUBJECT] + N'-' + [Filter1].[SEC_COURSE_NO])) AND ([GroupBy2].[K2] = [Filter1].[SEC_NO]) ) AS [Limit2]
)  AS [Project1]
ORDER BY [Project1].[SEC_SUBJECT] ASC, [Project1].[SEC_COURSE_NO] ASC, [Project1].[SEC_NO] ASC

这是有效的 linq-to-sql 命令

from section in db.COURSE_SECTIONS
join SectionEnrollments in (
    (from  STUDENT_ACAD_CRED in db.STUDENT_ACAD_CRED
        where STUDENT_ACAD_CRED.STC_TERM == "2012FL"
        group STUDENT_ACAD_CRED by new {
            STUDENT_ACAD_CRED.STC_COURSE_NAME,
            STUDENT_ACAD_CRED.STC_SECTION_NO } into g
        select new {
        g.Key.STC_COURSE_NAME,
        g.Key.STC_SECTION_NO,
        SectionEnrollment = (Int64?)g.Count()
    }))
        on section.SEC_SUBJECT + "-" + section.SEC_COURSE_NO + "-" + section.SEC_NO
            equals SectionEnrollments.STC_COURSE_NAME + "-" + SectionEnrollments.STC_SECTION_NO
where
    section.SEC_TERM == "2012FL"
orderby section.SEC_SUBJECT,
    section.SEC_COURSE_NO,
    section.SEC_NO
select new {
    section.SEC_SUBJECT,
    section.SEC_COURSE_NO,
    section.SEC_NO,
    SectionEnrollments.SectionEnrollment
};
4

0 回答 0