EF6,Linq to实体使用select new子句查询所有列

时间:2018-05-17 18:41:05

标签: sql-server entity-framework linq

不知道这里发生了什么,但即使我正在使用投影,我的查询也在使用每一列。

var students = (from x in db.vStudentProfiles
   where x.strLegalFirstName == searchDTO.FirstName &&
     x.strLegalMiddleName == searchDTO.MiddleName &&
     x.strLegalLastName == searchDTO.LastName &&
     x.datBirthDate == searchDTO.DateOfBirth &&
     x.intStudentStatusCd != 3 
   orderby x.intStudentStatusCd ascending, x.datCreated descending
    select new
    { 
        x.strLegalFirstName,
        x.strLegalLastName,
        x.strLegalMiddleName,
        x.datBirthDate,
        x.intStudentStatusCd,
        x.datCreated,
        x.strNBEN
    }
   ).ToList();

导致查询所有列:

    SELECT 
        [Project1].[C1] AS [C1], 
        [Project1].[strLegalFirstName] AS [strLegalFirstName], 
        [Project1].[strLegalLastName] AS [strLegalLastName], 
        [Project1].[strLegalMiddleName] AS [strLegalMiddleName], 
        [Project1].[datBirthDate] AS [datBirthDate], 
        [Project1].[intStudentStatusCd] AS [intStudentStatusCd], 
        [Project1].[datCreated] AS [datCreated], 
        [Project1].[strNBEN] AS [strNBEN]
        FROM ( SELECT 
            [Extent1].[strNBEN] AS [strNBEN], 
            [Extent1].[strLegalFirstName] AS [strLegalFirstName], 
            [Extent1].[strLegalMiddleName] AS [strLegalMiddleName], 
            [Extent1].[strLegalLastName] AS [strLegalLastName], 
            [Extent1].[datBirthDate] AS [datBirthDate], 
            [Extent1].[datCreated] AS [datCreated], 
            [Extent1].[intStudentStatusCd] AS [intStudentStatusCd], 
            1 AS [C1]
            FROM (SELECT 
        [vStudentProfile].[intStudentId] AS [intStudentId], 
        [vStudentProfile].[strStudentStatusDescE] AS [strStudentStatusDescE], 
        [vStudentProfile].[strSPID] AS [strSPID], 
        [vStudentProfile].[strSPIDPart1] AS [strSPIDPart1], 
        [vStudentProfile].[strSPIDPart3] AS [strSPIDPart3], 
        [vStudentProfile].[strSPIDPart4] AS [strSPIDPart4], 
........ bunch of other columns
        [vStudentProfile].[strStreetDirection] AS [strStreetDirection], 
        [vStudentProfile].[strProvince] AS [strProvince], 
        [vStudentProfile].[intStudentStatusCd] AS [intStudentStatusCd], 
        [vStudentProfile].[intSiteId] AS [intSiteId]
    FROM [dbo].[vStudentProfile] AS [vStudentProfile]) AS [Extent1]
        WHERE (([Extent1].[strLegalFirstName] = @p__linq__0) OR (([Extent1].[strLegalFirstName] IS NULL) AND (@p__linq__0 IS NULL))) AND (([Extent1].[strLegalMiddleName] = @p__linq__1) OR (([Extent1].[strLegalMiddleName] IS NULL) AND (@p__linq__1 IS NULL))) AND (([Extent1].[strLegalLastName] = @p__linq__2) OR (([Extent1].[strLegalLastName] IS NULL) AND (@p__linq__2 IS NULL))) AND ([Extent1].[datBirthDate] = @p__linq__3) AND (3 <> [Extent1].[intStudentStatusCd])
    )  AS [Project1]
    ORDER BY [Project1].[intStudentStatusCd] ASC, [Project1].[datCreated] DESC

我也试过

var students = (from x in db.vStudentProfiles
   let Child = new
   { 
       x.strLegalFirstName,
.....
   orderby x.intStudentStatusCd ascending, x.datCreated descending
   select Child
   ).ToList();

但正如我预期的那样没有什么区别。

将edmx用于映射的单个SQL Server视图。

1 个答案:

答案 0 :(得分:1)

  

不知道这里发生了什么,但即使我正在使用投影,我的查询也在使用每一列。

这就是EF生成查询的方式。 EF期望数据库引擎足够智能,可以忽略未使用的列。 SQL Server足够聪明。