EF Linq-SQL生成太多UNION(每个包含)

时间:2014-11-24 06:03:14

标签: c# linq entity-framework

我正在使用Code First方法,我的模型有一些收藏。

        /// <summary>
        /// Gets or sets projects.
        /// </summary>
        [JsonConverter(typeof(IDWriteListConverter))]
        public virtual ICollection<Project> Projects { get; set; }

        /// <summary>
        /// Gets or sets requests.
        /// </summary>
        [JsonConverter(typeof(IDWriteListConverter))]
        public virtual ICollection<Request> Requests { get; set; }

        /// <summary>
        /// Gets or sets comments.
        /// </summary>
        [JsonConverter(typeof(IDWriteListConverter))]
        public virtual ICollection<Comment> Comments { get; set; }

        /// <summary>
        /// Gets or sets roles.
        /// </summary>
         [JsonConverter(typeof(IDWriteListConverter))]
        public ICollection<SecurityRole> Roles { get; set; }

        /// <summary>
        /// Gets or sets tasks.
        /// </summary>
        [JsonConverter(typeof(IDWriteListConverter))]
        public ICollection<SecurityTask> Tasks { get; set; }

我所做的只是......

Context.Users.Include(...)。

在后台,我有一个如此庞大的查询。 ..

当我添加一个where子句时,只需4秒就可以选择。

SELECT 
    [UnionAll4].[C2] AS [C1], 
    -- removed columns
    FROM  (SELECT 
        [UnionAll3].[C1] AS [C1], 
           -- removed columns
        FROM  (SELECT 
            [UnionAll2].[C1] AS [C1], 
            [UnionAll2].[C2] AS [C2], 
               -- removed columns
            FROM  (SELECT 
                [UnionAll1].[C1] AS [C1], 
                   -- removed columns
                FROM  (SELECT 
                    CASE WHEN ([Join1].[ProjectID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1], 
                    [Extent1].[ID] AS [ID], 
                    [Extent1].[ID] AS [ID1], 
                    [Extent1].[Username] AS [Username], 
                    [Extent1].[Photo] AS [Photo], 
                    [Extent1].[PreferredName] AS [PreferredName], 
                    [Extent1].[FirstName] AS [FirstName], 
                    [Extent1].[LastName] AS [LastName], 
                    [Extent1].[Email] AS [Email], 
                    [Extent1].[DirectoryID] AS [DirectoryID], 
                    [Extent1].[DirectoryGuid] AS [DirectoryGuid], 
                    [Extent1].[DateLastLogin] AS [DateLastLogin], 
                    [Extent1].[DateCreated] AS [DateCreated], 
                    [Extent1].[DateModified] AS [DateModified], 
                    [Extent1].[BusinessUnit] AS [BusinessUnit], 
                    [Extent1].[Department] AS [Department], 
                    [Extent1].[JobTitle] AS [JobTitle], 
                    [Extent1].[WorkPhone] AS [WorkPhone], 
                    [Extent1].[AuthorID] AS [AuthorID], 
                    [Extent1].[CreatedBy] AS [CreatedBy], 
                    [Extent1].[UniqueIdentifier] AS [UniqueIdentifier], 
                    [Join1].[ID] AS [ID2], 
                    [Join1].[Description] AS [Description], 
                    [Join1].[AuthorID] AS [AuthorID1], 
                    [Join1].[DateCreated] AS [DateCreated1], 
                    [Join1].[CreatedBy] AS [CreatedBy1], 
                    [Join1].[DateModified] AS [DateModified1], 
                    [Join1].[UniqueIdentifier] AS [UniqueIdentifier1], 
                    CAST(NULL AS int) AS [C2], 
                    CAST(NULL AS int) AS [C3], 
                    CAST(NULL AS varchar(1)) AS [C4], 
                    CAST(NULL AS datetime2) AS [C5], 
                    CAST(NULL AS varchar(1)) AS [C6], 
                    CAST(NULL AS varchar(1)) AS [C7], 
                    CAST(NULL AS int) AS [C8], 
                    CAST(NULL AS bit) AS [C9], 
                    CAST(NULL AS int) AS [C10], 
                    CAST(NULL AS varchar(1)) AS [C11], 
                    CAST(NULL AS varchar(1)) AS [C12], 
                    CAST(NULL AS varchar(1)) AS [C13], 
                    CAST(NULL AS varchar(1)) AS [C14], 
                    CAST(NULL AS varchar(1)) AS [C15], 
                    CAST(NULL AS varchar(1)) AS [C16], 
                    CAST(NULL AS int) AS [C17], 
                    CAST(NULL AS int) AS [C18], 
                    CAST(NULL AS int) AS [C19], 
                    CAST(NULL AS int) AS [C20], 
                    CAST(NULL AS int) AS [C21], 
                    CAST(NULL AS datetime2) AS [C22], 
                    CAST(NULL AS datetime2) AS [C23], 
                    CAST(NULL AS datetime2) AS [C24], 
                    CAST(NULL AS bit) AS [C25], 
                    CAST(NULL AS bit) AS [C26], 
                    CAST(NULL AS int) AS [C27], 
                    CAST(NULL AS datetime2) AS [C28], 
                    CAST(NULL AS varchar(1)) AS [C29], 
                    CAST(NULL AS datetime2) AS [C30], 
                    CAST(NULL AS uniqueidentifier) AS [C31], 
                    CAST(NULL AS int) AS [C32], 
                    CAST(NULL AS int) AS [C33], 
                    CAST(NULL AS int) AS [C34], 
                    CAST(NULL AS int) AS [C35], 
                    CAST(NULL AS int) AS [C36], 
                    CAST(NULL AS varchar(1)) AS [C37], 
                    CAST(NULL AS int) AS [C38], 
                    CAST(NULL AS int) AS [C39], 
                    CAST(NULL AS datetime2) AS [C40], 
                    CAST(NULL AS varchar(1)) AS [C41], 
                    CAST(NULL AS datetime2) AS [C42], 
                    CAST(NULL AS uniqueidentifier) AS [C43], 
                    CAST(NULL AS int) AS [C44], 
                    CAST(NULL AS varchar(1)) AS [C45], 
                    CAST(NULL AS varchar(1)) AS [C46], 
                    CAST(NULL AS bit) AS [C47], 
                    CAST(NULL AS datetime2) AS [C48], 
                    CAST(NULL AS varchar(1)) AS [C49], 
                    CAST(NULL AS datetime2) AS [C50], 
                    CAST(NULL AS uniqueidentifier) AS [C51], 
                    CAST(NULL AS int) AS [C52], 
                    CAST(NULL AS int) AS [C53], 
                    CAST(NULL AS varchar(1)) AS [C54], 
                    CAST(NULL AS varchar(1)) AS [C55], 
                    CAST(NULL AS uniqueidentifier) AS [C56], 
                    CAST(NULL AS int) AS [C57]
                    FROM  [dbo].[users] AS [Extent1]
                    LEFT OUTER JOIN  (SELECT [Extent2].[ProjectID] AS [ProjectID], [Extent2].[UserID] AS [UserID], [Extent3].[ID] AS [ID], [Extent3].[Description] AS [Description], [Extent3].[AuthorID] AS [AuthorID], [Extent3].[DateCreated] AS [DateCreated], [Extent3].[CreatedBy] AS [CreatedBy], [Extent3].[DateModified] AS [DateModified], [Extent3].[UniqueIdentifier] AS [UniqueIdentifier]
                        FROM  [dbo].[userprojects] AS [Extent2]
                        INNER JOIN [dbo].[projects] AS [Extent3] ON [Extent3].[ID] = [Extent2].[ProjectID] ) AS [Join1] ON [Extent1].[ID] = [Join1].[UserID]
                UNION ALL
                    SELECT 
                    2 AS [C1], 
                    [Extent4].[ID] AS [ID], 
                    [Extent4].[ID] AS [ID1], 
                    [Extent4].[Username] AS [Username], 
                    [Extent4].[Photo] AS [Photo], 
                    [Extent4].[PreferredName] AS [PreferredName], 
                    [Extent4].[FirstName] AS [FirstName], 
                    [Extent4].[LastName] AS [LastName], 
                    [Extent4].[Email] AS [Email], 
                    [Extent4].[DirectoryID] AS [DirectoryID], 
                    [Extent4].[DirectoryGuid] AS [DirectoryGuid], 
                    [Extent4].[DateLastLogin] AS [DateLastLogin], 
                    [Extent4].[DateCreated] AS [DateCreated], 
                    [Extent4].[DateModified] AS [DateModified], 
                    [Extent4].[BusinessUnit] AS [BusinessUnit], 
                    [Extent4].[Department] AS [Department], 
                    [Extent4].[JobTitle] AS [JobTitle], 
                    [Extent4].[WorkPhone] AS [WorkPhone], 
                    [Extent4].[AuthorID] AS [AuthorID], 
                    [Extent4].[CreatedBy] AS [CreatedBy], 
                    [Extent4].[UniqueIdentifier] AS [UniqueIdentifier], 
                    CAST(NULL AS int) AS [C2], 
                    CAST(NULL AS varchar(1)) AS [C3], 
                    CAST(NULL AS int) AS [C4], 
                    CAST(NULL AS datetime2) AS [C5], 
                    CAST(NULL AS varchar(1)) AS [C6], 
                    CAST(NULL AS datetime2) AS [C7], 
                    CAST(NULL AS uniqueidentifier) AS [C8], 
                    [Extent5].[ID] AS [ID2], 
                    [Extent5].[ID] AS [ID3], 
                    [Extent5].[Title] AS [Title], 
                    [Extent5].[DateDue] AS [DateDue], 
                    [Extent5].[Department] AS [Department1], 
                    [Extent5].[BusinessUnit] AS [BusinessUnit1], 
                    [Extent5].[PortfolioID] AS [PortfolioID], 
                    [Extent5].[PreviouslyDone] AS [PreviouslyDone], 
                    [Extent5].[Status] AS [Status], 
                    [Extent5].[Problem] AS [Problem], 
                    [Extent5].[CustomerSatisfaction] AS [CustomerSatisfaction], 
                    [Extent5].[IncrementalRevenue] AS [IncrementalRevenue], 
                    [Extent5].[CostReduction] AS [CostReduction], 
                    [Extent5].[RequestCode] AS [RequestCode], 
                    [Extent5].[Complexity] AS [Complexity], 
                    [Extent5].[AssigneeID] AS [AssigneeID], 
                    [Extent5].[OwnerID] AS [OwnerID], 
                    [Extent5].[OutcomeID] AS [OutcomeID], 
                    [Extent5].[ScopeID] AS [ScopeID], 
                    [Extent5].[FeedbackID] AS [FeedbackID], 
                    [Extent5].[DateCompleted] AS [DateCompleted], 
                    [Extent5].[DateClosed] AS [DateClosed], 
                    [Extent5].[DateRequestedDue] AS [DateRequestedDue], 
                    [Extent5].[Urgent] AS [Urgent], 
                    [Extent5].[Confidential] AS [Confidential], 
                    [Extent5].[AuthorID] AS [AuthorID1], 
                    [Extent5].[DateCreated] AS [DateCreated1], 
                    [Extent5].[CreatedBy] AS [CreatedBy1], 
                    [Extent5].[DateModified] AS [DateModified1], 
                    [Extent5].[UniqueIdentifier] AS [UniqueIdentifier1], 
                    [Extent5].[Project_ID] AS [Project_ID], 
                    CAST(NULL AS int) AS [C9], 
                    CAST(NULL AS int) AS [C10], 
                    CAST(NULL AS int) AS [C11], 
                    CAST(NULL AS int) AS [C12], 
                    CAST(NULL AS varchar(1)) AS [C13], 
                    CAST(NULL AS int) AS [C14], 
                    CAST(NULL AS int) AS [C15], 
                    CAST(NULL AS datetime2) AS [C16], 
                    CAST(NULL AS varchar(1)) AS [C17], 
                    CAST(NULL AS datetime2) AS [C18], 
                    CAST(NULL AS uniqueidentifier) AS [C19], 
                    CAST(NULL AS int) AS [C20], 
                    CAST(NULL AS varchar(1)) AS [C21], 
                    CAST(NULL AS varchar(1)) AS [C22], 
                    CAST(NULL AS bit) AS [C23], 
                    CAST(NULL AS datetime2) AS [C24], 
                    CAST(NULL AS varchar(1)) AS [C25], 
                    CAST(NULL AS datetime2) AS [C26], 
                    CAST(NULL AS uniqueidentifier) AS [C27], 
                    CAST(NULL AS int) AS [C28], 
                    CAST(NULL AS int) AS [C29], 
                    CAST(NULL AS varchar(1)) AS [C30], 
                    CAST(NULL AS varchar(1)) AS [C31], 
                    CAST(NULL AS uniqueidentifier) AS [C32], 
                    CAST(NULL AS int) AS [C33]
                    FROM  [dbo].[users] AS [Extent4]
                    INNER JOIN [dbo].[requests] AS [Extent5] ON [Extent4].[ID] = [Extent5].[AuthorID]) AS [UnionAll1]
            UNION ALL
                SELECT 
                3 AS [C1], 
                [Extent6].[ID] AS [ID], 
                [Extent6].[ID] AS [ID1], 
                [Extent6].[Username] AS [Username], 
                [Extent6].[Photo] AS [Photo], 
                [Extent6].[PreferredName] AS [PreferredName], 
                [Extent6].[FirstName] AS [FirstName], 
                [Extent6].[LastName] AS [LastName], 
                [Extent6].[Email] AS [Email], 
                [Extent6].[DirectoryID] AS [DirectoryID], 
                [Extent6].[DirectoryGuid] AS [DirectoryGuid], 
                [Extent6].[DateLastLogin] AS [DateLastLogin], 
                [Extent6].[DateCreated] AS [DateCreated], 
                [Extent6].[DateModified] AS [DateModified], 
                [Extent6].[BusinessUnit] AS [BusinessUnit], 
                [Extent6].[Department] AS [Department], 
                [Extent6].[JobTitle] AS [JobTitle], 
                [Extent6].[WorkPhone] AS [WorkPhone], 
                [Extent6].[AuthorID] AS [AuthorID], 
                [Extent6].[CreatedBy] AS [CreatedBy], 
                [Extent6].[UniqueIdentifier] AS [UniqueIdentifier], 
                CAST(NULL AS int) AS [C2], 
                CAST(NULL AS varchar(1)) AS [C3], 
                CAST(NULL AS int) AS [C4], 
                CAST(NULL AS datetime2) AS [C5], 
                CAST(NULL AS varchar(1)) AS [C6], 
                CAST(NULL AS datetime2) AS [C7], 
                CAST(NULL AS uniqueidentifier) AS [C8], 
                CAST(NULL AS int) AS [C9], 
                CAST(NULL AS int) AS [C10], 
                CAST(NULL AS varchar(1)) AS [C11], 
                CAST(NULL AS datetime2) AS [C12], 
                CAST(NULL AS varchar(1)) AS [C13], 
                CAST(NULL AS varchar(1)) AS [C14], 
                CAST(NULL AS int) AS [C15], 
                CAST(NULL AS bit) AS [C16], 
                CAST(NULL AS int) AS [C17], 
                CAST(NULL AS varchar(1)) AS [C18], 
                CAST(NULL AS varchar(1)) AS [C19], 
                CAST(NULL AS varchar(1)) AS [C20], 
                CAST(NULL AS varchar(1)) AS [C21], 
                CAST(NULL AS varchar(1)) AS [C22], 
                CAST(NULL AS varchar(1)) AS [C23], 
                CAST(NULL AS int) AS [C24], 
                CAST(NULL AS int) AS [C25], 
                CAST(NULL AS int) AS [C26], 
                CAST(NULL AS int) AS [C27], 
                CAST(NULL AS int) AS [C28], 
                CAST(NULL AS datetime2) AS [C29], 
                CAST(NULL AS datetime2) AS [C30], 
                CAST(NULL AS datetime2) AS [C31], 
                CAST(NULL AS bit) AS [C32], 
                CAST(NULL AS bit) AS [C33], 
                CAST(NULL AS int) AS [C34], 
                CAST(NULL AS datetime2) AS [C35], 
                CAST(NULL AS varchar(1)) AS [C36], 
                CAST(NULL AS datetime2) AS [C37], 
                CAST(NULL AS uniqueidentifier) AS [C38], 
                CAST(NULL AS int) AS [C39], 
                [Extent7].[ID] AS [ID2], 
                [Extent7].[RequestID] AS [RequestID], 
                [Extent7].[ProjectID] AS [ProjectID], 
                [Extent7].[UserID] AS [UserID], 
                [Extent7].[Description] AS [Description], 
                [Extent7].[Category] AS [Category], 
                [Extent7].[AuthorID] AS [AuthorID1], 
                [Extent7].[DateCreated] AS [DateCreated1], 
                [Extent7].[CreatedBy] AS [CreatedBy1], 
                [Extent7].[DateModified] AS [DateModified1], 
                [Extent7].[UniqueIdentifier] AS [UniqueIdentifier1], 
                CAST(NULL AS int) AS [C40], 
                CAST(NULL AS varchar(1)) AS [C41], 
                CAST(NULL AS varchar(1)) AS [C42], 
                CAST(NULL AS bit) AS [C43], 
                CAST(NULL AS datetime2) AS [C44], 
                CAST(NULL AS varchar(1)) AS [C45], 
                CAST(NULL AS datetime2) AS [C46], 
                CAST(NULL AS uniqueidentifier) AS [C47], 
                CAST(NULL AS int) AS [C48], 
                CAST(NULL AS int) AS [C49], 
                CAST(NULL AS varchar(1)) AS [C50], 
                CAST(NULL AS varchar(1)) AS [C51], 
                CAST(NULL AS uniqueidentifier) AS [C52], 
                CAST(NULL AS int) AS [C53]
                FROM  [dbo].[users] AS [Extent6]
                INNER JOIN [dbo].[comments] AS [Extent7] ON [Extent6].[ID] = [Extent7].[AuthorID]) AS [UnionAll2]
        UNION ALL
            SELECT 
            4 AS [C1], 
            [Extent8].[ID] AS [ID], 
            [Extent8].[ID] AS [ID1], 
            [Extent8].[Username] AS [Username], 
            [Extent8].[Photo] AS [Photo], 
            [Extent8].[PreferredName] AS [PreferredName], 
            [Extent8].[FirstName] AS [FirstName], 
            [Extent8].[LastName] AS [LastName], 
            [Extent8].[Email] AS [Email], 
            [Extent8].[DirectoryID] AS [DirectoryID], 
            [Extent8].[DirectoryGuid] AS [DirectoryGuid], 
            [Extent8].[DateLastLogin] AS [DateLastLogin], 
            [Extent8].[DateCreated] AS [DateCreated], 
            [Extent8].[DateModified] AS [DateModified], 
            [Extent8].[BusinessUnit] AS [BusinessUnit], 
            [Extent8].[Department] AS [Department], 
            [Extent8].[JobTitle] AS [JobTitle], 
            [Extent8].[WorkPhone] AS [WorkPhone], 
            [Extent8].[AuthorID] AS [AuthorID], 
            [Extent8].[CreatedBy] AS [CreatedBy], 
            [Extent8].[UniqueIdentifier] AS [UniqueIdentifier], 
            CAST(NULL AS int) AS [C2], 
            CAST(NULL AS varchar(1)) AS [C3], 
            CAST(NULL AS int) AS [C4], 
            CAST(NULL AS datetime2) AS [C5], 
            CAST(NULL AS varchar(1)) AS [C6], 
            CAST(NULL AS datetime2) AS [C7], 
            CAST(NULL AS uniqueidentifier) AS [C8], 
            CAST(NULL AS int) AS [C9], 
            CAST(NULL AS int) AS [C10], 
            CAST(NULL AS varchar(1)) AS [C11], 
            CAST(NULL AS datetime2) AS [C12], 
            CAST(NULL AS varchar(1)) AS [C13], 
            CAST(NULL AS varchar(1)) AS [C14], 
            CAST(NULL AS int) AS [C15], 
            CAST(NULL AS bit) AS [C16], 
            CAST(NULL AS int) AS [C17], 
            CAST(NULL AS varchar(1)) AS [C18], 
            CAST(NULL AS varchar(1)) AS [C19], 
            CAST(NULL AS varchar(1)) AS [C20], 
            CAST(NULL AS varchar(1)) AS [C21], 
            CAST(NULL AS varchar(1)) AS [C22], 
            CAST(NULL AS varchar(1)) AS [C23], 
            CAST(NULL AS int) AS [C24], 
            CAST(NULL AS int) AS [C25], 
            CAST(NULL AS int) AS [C26], 
            CAST(NULL AS int) AS [C27], 
            CAST(NULL AS int) AS [C28], 
            CAST(NULL AS datetime2) AS [C29], 
            CAST(NULL AS datetime2) AS [C30], 
            CAST(NULL AS datetime2) AS [C31], 
            CAST(NULL AS bit) AS [C32], 
            CAST(NULL AS bit) AS [C33], 
            CAST(NULL AS int) AS [C34], 
            CAST(NULL AS datetime2) AS [C35], 
            CAST(NULL AS varchar(1)) AS [C36], 
            CAST(NULL AS datetime2) AS [C37], 
            CAST(NULL AS uniqueidentifier) AS [C38], 
            CAST(NULL AS int) AS [C39], 
            CAST(NULL AS int) AS [C40], 
            CAST(NULL AS int) AS [C41], 
            CAST(NULL AS int) AS [C42], 
            CAST(NULL AS int) AS [C43], 
            CAST(NULL AS varchar(1)) AS [C44], 
            CAST(NULL AS int) AS [C45], 
            CAST(NULL AS int) AS [C46], 
            CAST(NULL AS datetime2) AS [C47], 
            CAST(NULL AS varchar(1)) AS [C48], 
            CAST(NULL AS datetime2) AS [C49], 
            CAST(NULL AS uniqueidentifier) AS [C50], 
            [Join5].[ID] AS [ID2], 
            [Join5].[Name] AS [Name], 
            [Join5].[Description] AS [Description], 
            [Join5].[CustomRole] AS [CustomRole], 
            [Join5].[DateCreated] AS [DateCreated1], 
            [Join5].[CreatedBy] AS [CreatedBy1], 
            [Join5].[DateModified] AS [DateModified1], 
            [Join5].[UniqueIdentifier] AS [UniqueIdentifier1], 
            CAST(NULL AS int) AS [C51], 
            CAST(NULL AS int) AS [C52], 
            CAST(NULL AS varchar(1)) AS [C53], 
            CAST(NULL AS varchar(1)) AS [C54], 
            CAST(NULL AS uniqueidentifier) AS [C55], 
            CAST(NULL AS int) AS [C56]
            FROM  [dbo].[users] AS [Extent8]
            INNER JOIN  (SELECT [Extent9].[UserID] AS [UserID], [Extent10].[ID] AS [ID], [Extent10].[Name] AS [Name], [Extent10].[Description] AS [Description], [Extent10].[CustomRole] AS [CustomRole], [Extent10].[DateCreated] AS [DateCreated], [Extent10].[CreatedBy] AS [CreatedBy], [Extent10].[DateModified] AS [DateModified], [Extent10].[UniqueIdentifier] AS [UniqueIdentifier]
                FROM  [dbo].[securityuserroles] AS [Extent9]
                INNER JOIN [dbo].[securityroles] AS [Extent10] ON [Extent10].[ID] = [Extent9].[RoleID] ) AS [Join5] ON [Extent8].[ID] = [Join5].[UserID]) AS [UnionAll3]
    UNION ALL
        SELECT 
        5 AS [C1], 
        [Extent11].[ID] AS [ID], 
        [Extent11].[ID] AS [ID1], 
        [Extent11].[Username] AS [Username], 
        [Extent11].[Photo] AS [Photo], 
        [Extent11].[PreferredName] AS [PreferredName], 
        [Extent11].[FirstName] AS [FirstName], 
        [Extent11].[LastName] AS [LastName], 
        [Extent11].[Email] AS [Email], 
        [Extent11].[DirectoryID] AS [DirectoryID], 
        [Extent11].[DirectoryGuid] AS [DirectoryGuid], 
        [Extent11].[DateLastLogin] AS [DateLastLogin], 
        [Extent11].[DateCreated] AS [DateCreated], 
        [Extent11].[DateModified] AS [DateModified], 
        [Extent11].[BusinessUnit] AS [BusinessUnit], 
        [Extent11].[Department] AS [Department], 
        [Extent11].[JobTitle] AS [JobTitle], 
        [Extent11].[WorkPhone] AS [WorkPhone], 
        [Extent11].[AuthorID] AS [AuthorID], 
        [Extent11].[CreatedBy] AS [CreatedBy], 
        [Extent11].[UniqueIdentifier] AS [UniqueIdentifier], 
        CAST(NULL AS int) AS [C2], 
        CAST(NULL AS varchar(1)) AS [C3], 
        CAST(NULL AS int) AS [C4], 
        CAST(NULL AS datetime2) AS [C5], 
        CAST(NULL AS varchar(1)) AS [C6], 
        CAST(NULL AS datetime2) AS [C7], 
        CAST(NULL AS uniqueidentifier) AS [C8], 
        CAST(NULL AS int) AS [C9], 
        CAST(NULL AS int) AS [C10], 
        CAST(NULL AS varchar(1)) AS [C11], 
        CAST(NULL AS datetime2) AS [C12], 
        CAST(NULL AS varchar(1)) AS [C13], 
        CAST(NULL AS varchar(1)) AS [C14], 
        CAST(NULL AS int) AS [C15], 
        CAST(NULL AS bit) AS [C16], 
        CAST(NULL AS int) AS [C17], 
        CAST(NULL AS varchar(1)) AS [C18], 
        CAST(NULL AS varchar(1)) AS [C19], 
        CAST(NULL AS varchar(1)) AS [C20], 
        CAST(NULL AS varchar(1)) AS [C21], 
        CAST(NULL AS varchar(1)) AS [C22], 
        CAST(NULL AS varchar(1)) AS [C23], 
        CAST(NULL AS int) AS [C24], 
        CAST(NULL AS int) AS [C25], 
        CAST(NULL AS int) AS [C26], 
        CAST(NULL AS int) AS [C27], 
        CAST(NULL AS int) AS [C28], 
        CAST(NULL AS datetime2) AS [C29], 
        CAST(NULL AS datetime2) AS [C30], 
        CAST(NULL AS datetime2) AS [C31], 
        CAST(NULL AS bit) AS [C32], 
        CAST(NULL AS bit) AS [C33], 
        CAST(NULL AS int) AS [C34], 
        CAST(NULL AS datetime2) AS [C35], 
        CAST(NULL AS varchar(1)) AS [C36], 
        CAST(NULL AS datetime2) AS [C37], 
        CAST(NULL AS uniqueidentifier) AS [C38], 
        CAST(NULL AS int) AS [C39], 
        CAST(NULL AS int) AS [C40], 
        CAST(NULL AS int) AS [C41], 
        CAST(NULL AS int) AS [C42], 
        CAST(NULL AS int) AS [C43], 
        CAST(NULL AS varchar(1)) AS [C44], 
        CAST(NULL AS int) AS [C45], 
        CAST(NULL AS int) AS [C46], 
        CAST(NULL AS datetime2) AS [C47], 
        CAST(NULL AS varchar(1)) AS [C48], 
        CAST(NULL AS datetime2) AS [C49], 
        CAST(NULL AS uniqueidentifier) AS [C50], 
        CAST(NULL AS int) AS [C51], 
        CAST(NULL AS varchar(1)) AS [C52], 
        CAST(NULL AS varchar(1)) AS [C53], 
        CAST(NULL AS bit) AS [C54], 
        CAST(NULL AS datetime2) AS [C55], 
        CAST(NULL AS varchar(1)) AS [C56], 
        CAST(NULL AS datetime2) AS [C57], 
        CAST(NULL AS uniqueidentifier) AS [C58], 
        [Extent12].[ID] AS [ID2], 
        [Extent12].[ID] AS [ID3], 
        [Extent12].[Name] AS [Name], 
        [Extent12].[Description] AS [Description], 
        [Extent12].[UniqueIdentifier] AS [UniqueIdentifier1], 
        [Extent12].[User_ID] AS [User_ID]
        FROM  [dbo].[users] AS [Extent11]
        INNER JOIN [dbo].[SecurityTasks] AS [Extent12] ON [Extent11].[ID] = [Extent12].[User_ID]) AS [UnionAll4]
    ORDER BY [UnionAll4].[C3] ASC, [UnionAll4].[C1] ASC

更新:

基于我无法使用Join的建议,因为我有一个GenericRepository,它使用Include方法来包含任何类型的集合属性。

以下是供您参考的代码:

public virtual IQueryable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "", int skip = 0, int take = 0)
        {
            IQueryable<TEntity> query = _databaseSet;

            if (query != null)
            {
                if (filter != null)
                {
                    query = query.Where(filter);
                }

                if (includeProperties == null)
                {
                    includeProperties = string.Empty;
                }

                query = includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Aggregate(query, (current, property) => current.Include(property));

                if (orderBy != null)
                {
                    query = orderBy(query);

                    if (take != 0)
                    {
                        query = query.Skip(skip).Take(take);
                    }
                    else if (skip > 0)
                    {
                        query = query.Skip(skip);
                    }
                }
            }

            return query;
        }

0 个答案:

没有答案