如何避免Entity Framework 6在生成的查询中添加ORDER BY

时间:2014-12-16 16:01:13

标签: sql entity-framework sql-order-by entity-framework-6

使用EF 6我选择的结果集如下。数据由id选择,然后从嵌套对象获取信息,这些信息是相关表。我的问题是生成的查询正在添加OrderBy,我不需要如何将EF设置为不对我的结果集进行排序?

using (var repository = new Repository(uow))
      {
        var result= repository.FindAllIncluding(includeParams)
                              .Where(tr=>tr.Id==Id)
                              .FirstOrDefault();
        FillModel(modelToReturn, result);
      }

生成的SQL的片段是这样的,最后你可以看到它是由ITreatmenetRequestId订购的:

DECLARE @p__linq__0 uniqueidentifier = 'E72C5916-5CF1-4FDD-AF59-CCCCCCCCCC'

SELECT 
    [Project2].[statecode] AS [statecode], 
    ...
    [Project2].[EffectiveTo] AS [EffectiveTo], 
    [Project2].[PayerMemberNumber] AS [PayerMemberNumber], 
    [Project2].[LineOfBusiness] AS [LineOfBusiness], 
    [Project2].[statecode2] AS [statecode2]
    FROM ( SELECT 
        ...
        [Limit1].[Gender] AS [Gender], 
        [Limit1].[DateOfBirth] AS [DateOfBirth], 
        [Limit1].[WriteInStatus] AS [WriteInStatus], 
        [Limit1].[statecode1] AS [statecode1], 
        [Join2].[MemberPayerEligibilityId] AS [MemberPayerEligibilityId], 
        [Join2].[MemberId1] AS [MemberId2], 
        [Join2].[PayerId] AS [PayerId1], 
        [Join2].[EffectiveFrom] AS [EffectiveFrom], 
        [Join2].[EffectiveTo] AS [EffectiveTo], 
        [Join2].[PayerMemberNumber] AS [PayerMemberNumber], 
        [Join2].[LineOfBusiness] AS [LineOfBusiness], 
        [Join2].[statecode1] AS [statecode2], 
        CASE WHEN ([Join2].[MemberPayerEligibilityId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
        FROM   (SELECT TOP (1) 
          ...
            [Extent2].[MiddleName] AS [MiddleName], 
            [Extent2].[LastName] AS [LastName], 
            [Extent2].[Gender] AS [Gender], 
            [Extent2].[DateOfBirth] AS [DateOfBirth], 
            [Extent2].[WriteInStatus] AS [WriteInStatus], 
            [Extent2].[statecode] AS [statecode1]
            FROM  [dbo].[TreatmentRequestBase] AS [Extent1]
            LEFT OUTER JOIN [dbo].[MemberBase] AS [Extent2] ON [Extent1].[MemberId] = [Extent2].[MemberId]
            WHERE [Extent1].[TreatmentRequestId] = @p__linq__0 ) AS [Limit1]
        LEFT OUTER JOIN
        (SELECT [Extent3].[MemberPayerEligibilityId] AS [MemberPayerEligibilityId],
            [Extent3].[MemberId] AS [MemberId1], [Extent3].[PayerId] AS [PayerId], [Extent3].[EffectiveFrom] AS [EffectiveFrom],
            [Extent3].[EffectiveTo] AS [EffectiveTo], [Extent3].[PayerMemberNumber] AS [PayerMemberNumber],
            [Extent3].[LineOfBusiness] AS [LineOfBusiness], [Extent3].[statecode] AS [statecode1]
            ,[Extent4].[MemberId] AS [MemberId2]
            FROM  [dbo].[MemberPayerEligibilityBase] AS [Extent3]
            INNER JOIN [dbo].[MemberBase] AS [Extent4] ON [Extent4].[MemberId] = [Extent3].[MemberId]
            ) AS [Join2] ON [Limit1].[MemberId] = [Join2].[MemberId1]
    )  AS [Project2]

    ORDER BY [Project2].[TreatmentRequestId] ASC, [Project2].[MemberId1] ASC, [Project2].[C1] ASC

1 个答案:

答案 0 :(得分:0)

你要求“FirstOrDefault()”......没有顺序,“First”这个词毫无意义。请尝试使用“SingleOrDefault()”。当然,有一些限制。如果查询返回的行数超过0或1,则会产生错误。