如何规范化LINQ查询

时间:2016-02-28 01:39:38

标签: sql linq optimization

我有这个linq查询,其中包含7个表来获取用户事务,

var transLst = from trans in context.CommunicationTransactions
               join cc in context.CommunicationCases on trans.CommunicationCaseId equals cc.CommunicationCaseId
               join cw in context.CommunicationWays on trans.CommunicationWayId equals cw.CommunicationWayId
               join mem in context.Members on trans.MemeberId equals mem.MemberId
               join us in context.Users on trans.UserId equals us.UserId
               join loc in context.Locations on mem.MemberLocationId equals loc.LocationId
               join ar in context.Areas on loc.LocationAreaId equals ar.AreaId
               where
                       (AreasIdsList.Count.Equals(0) || AreasIdsList.Contains(ar.AreaId)) &&
                       trans.CommunicationDate == (from transSub in context.CommunicationTransactions
                                                   where transSub.MemeberId == trans.MemeberId && transSub.UserId == trans.UserId
                                                   group transSub by transSub.UserId into t
                                                   select new { CommunicationDate = t.Max(d => d.CommunicationDate) }).FirstOrDefault().CommunicationDate
                       select new
                                {
                                    cc.CommunicationCaseDescription,
                                    cw.CommunicationDescription,
                                    mem.MemberId,
                                    mem.MemberName,
                                    mem.MemberMobile,
                                    mem.MemberMobile1,
                                    mem.MemberMobile2,
                                    mem.MemberEmail,
                                    mem.MemberOwnerName,
                                    us.UserId,
                                    UserFullName = (us.FirstName + " " + us.SecondName + " " + us.ThirdName),
                                    trans.CommunicationDate,
                                    trans.AddedDate,
                                    loc.LocationName,
                                    ar.AreaName,
                                    trans.TransId,
                                    trans.Comment,
                                    trans.CommunicationsGroupId,
                                    trans.CommunicationCaseId,
                                    trans.CommunicationWayId
                                };

当我尝试使用SQL事件探查器跟踪它时,我发现它被翻译为

exec sp_executesql N'SELECT 
[Project2].[TransId] AS [TransId], 
[Project2].[CommunicationCaseDescription] AS [CommunicationCaseDescription], 
[Project2].[CommunicationDescription] AS [CommunicationDescription], 
[Project2].[MemberId] AS [MemberId], 
[Project2].[MemberName] AS [MemberName], 
[Project2].[MemberMobile] AS [MemberMobile], 
[Project2].[MemberMobile1] AS [MemberMobile1], 
[Project2].[MemberMobile2] AS [MemberMobile2], 
[Project2].[MemberEmail] AS [MemberEmail], 
[Project2].[MemberOwnerName] AS [MemberOwnerName], 
[Project2].[UserId] AS [UserId], 
CASE WHEN ([Project2].[FirstName] IS NULL) THEN N'''' ELSE [Project2].[FirstName] END + N'' '' + 
CASE WHEN ([Project2].[SecondName] IS NULL) THEN N'''' ELSE [Project2].[SecondName] END + N'' '' + 
    CASE WHEN ([Project2].[ThirdName] IS NULL) THEN N'''' ELSE [Project2].[ThirdName] END AS [C1], 
[Project2].[CommunicationDate] AS [CommunicationDate], 
[Project2].[AddedDate] AS [AddedDate], 
[Project2].[LocationName] AS [LocationName], 
[Project2].[AreaName] AS [AreaName], 
[Project2].[Comment] AS [Comment], 
[Project2].[CommunicationsGroupId] AS [CommunicationsGroupId], 
[Project2].[CommunicationCaseId] AS [CommunicationCaseId], 
[Project2].[CommunicationWayId] AS [CommunicationWayId]
FROM ( SELECT 
    [Extent1].[TransId] AS [TransId], 
    [Extent1].[CommunicationCaseId] AS [CommunicationCaseId], 
    [Extent1].[CommunicationDate] AS [CommunicationDate], 
    [Extent1].[CommunicationWayId] AS [CommunicationWayId], 
    [Extent1].[Comment] AS [Comment], 
    [Extent1].[AddedDate] AS [AddedDate], 
    [Extent1].[CommunicationsGroupId] AS [CommunicationsGroupId], 
    [Extent2].[CommunicationCaseDescription] AS [CommunicationCaseDescription], 
    [Extent3].[CommunicationDescription] AS [CommunicationDescription], 
    [Extent4].[MemberId] AS [MemberId], 
    [Extent4].[MemberName] AS [MemberName], 
    [Extent4].[MemberMobile] AS [MemberMobile], 
    [Extent4].[MemberEmail] AS [MemberEmail], 
    [Extent4].[MemberOwnerName] AS [MemberOwnerName], 
    [Extent4].[MemberMobile1] AS [MemberMobile1], 
    [Extent4].[MemberMobile2] AS [MemberMobile2], 
    [Extent5].[UserId] AS [UserId], 
    [Extent5].[FirstName] AS [FirstName], 
    [Extent5].[SecondName] AS [SecondName], 
    [Extent5].[ThirdName] AS [ThirdName], 
    [Extent6].[LocationName] AS [LocationName], 
    [Extent7].[AreaName] AS [AreaName], 
    (SELECT TOP (1) 
        [GroupBy1].[A1] AS [C1]
        FROM ( SELECT 
            [Extent8].[UserId] AS [K1], 
            MAX([Extent8].[CommunicationDate]) AS [A1]
            FROM [dbo].[CommunicationTransactions] AS [Extent8]
            WHERE ([Extent8].[MemeberId] = [Extent1].[MemeberId]) AND ([Extent8].[UserId] = [Extent1].[UserId])
            GROUP BY [Extent8].[UserId]
        )  AS [GroupBy1]) AS [C1]
    FROM       [dbo].[CommunicationTransactions] AS [Extent1]
    INNER JOIN [dbo].[CommunicationCases] AS [Extent2] ON [Extent1].[CommunicationCaseId] = [Extent2].[CommunicationCaseId]
    INNER JOIN [dbo].[CommunicationWays] AS [Extent3] ON [Extent1].[CommunicationWayId] = [Extent3].[CommunicationWayId]
    INNER JOIN [dbo].[Members] AS [Extent4] ON [Extent1].[MemeberId] = [Extent4].[MemberId]
    INNER JOIN [dbo].[Users] AS [Extent5] ON [Extent1].[UserId] = [Extent5].[UserId]
    INNER JOIN [dbo].[Locations] AS [Extent6] ON [Extent4].[MemberLocationId] = [Extent6].[LocationId]
    INNER JOIN [dbo].[Areas] AS [Extent7] ON [Extent6].[LocationAreaId] = [Extent7].[AreaId]
)  AS [Project2]
WHERE (0 = @p__linq__0) AND ([Project2].[CommunicationDate] = [Project2].[C1])',N'@p__linq__0 int',@p__linq__0=0

我在这个声明中做错了这样做了吗?编辑我的代码以进行增强的任何帮助?或者这是Linq To Sql的最佳输出?

0 个答案:

没有答案