Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 82.40 KB | None | 0 0
  1. exec sp_executesql N'SELECT
  2.    [UnionAll2].[SecurityLevel] AS [C1],
  3.    [UnionAll2].[IsDriverEnabled] AS [C2],
  4.    [UnionAll2].[MaximumTrialLossRuns] AS [C3],
  5.    [UnionAll2].[InterlocationSharingLevel] AS [C4],
  6.    [UnionAll2].[DaysPriorOrderingLossRunnNotification] AS [C5],
  7.    [UnionAll2].[SendEmailsFlag] AS [C6],
  8.    [UnionAll2].[AllowViewAcordXml] AS [C7],
  9.    [UnionAll2].[IsAppulateTeam] AS [C8],
  10.    [UnionAll2].[UplinkOcrEnabled] AS [C9],
  11.    [UnionAll2].[LoginLockingEnabled] AS [C10],
  12.    [UnionAll2].[UplinkSettings] AS [C11],
  13.    [UnionAll2].[ShortName] AS [C12],
  14.    [UnionAll2].[InsuredDeletionRestrictionType] AS [C13],
  15.    [UnionAll2].[AllowNonAdminUsersToSelectDeselectMarkets] AS [C14],
  16.    [UnionAll2].[IsDecliningSubmissionAllowed] AS [C15],
  17.    [UnionAll2].[EmsRestrictedDesignations] AS [C16],
  18.    [UnionAll2].[EmsAllowUserControlDesignations] AS [C17],
  19.    [UnionAll2].[IsVisibleForMarkets] AS [C18],
  20.    [UnionAll2].[Id] AS [C19],
  21.    [UnionAll2].[Id1] AS [C20],
  22.    [UnionAll2].[Id2] AS [C21],
  23.    [UnionAll2].[MarketId] AS [C22],
  24.    [UnionAll2].[CompanyId] AS [C23],
  25.    [UnionAll2].[MarketId1] AS [C24],
  26.    [UnionAll2].[CompanyId1] AS [C25],
  27.    [UnionAll2].[C3] AS [C26],
  28.    [UnionAll2].[Id3] AS [C27],
  29.    [UnionAll2].[Name] AS [C28],
  30.    [UnionAll2].[DateRegistered] AS [C29],
  31.    [UnionAll2].[City] AS [C30],
  32.    [UnionAll2].[StateId] AS [C31],
  33.    [UnionAll2].[Phone] AS [C32],
  34.    [UnionAll2].[C4] AS [C33],
  35.    [UnionAll2].[LastLoginTime] AS [C34],
  36.    [UnionAll2].[C5] AS [C35],
  37.    [UnionAll2].[IsTesting] AS [C36],
  38.    [UnionAll2].[C6] AS [C37],
  39.    [UnionAll2].[C1] AS [C38],
  40.    [UnionAll2].[Id4] AS [C39],
  41.    [UnionAll2].[MarketId2] AS [C40],
  42.    [UnionAll2].[Name1] AS [C41],
  43.    [UnionAll2].[StateId1] AS [C42],
  44.    [UnionAll2].[NotificationEmail] AS [C43],
  45.    [UnionAll2].[C7] AS [C44],
  46.    [UnionAll2].[C8] AS [C45]
  47.    FROM  (SELECT
  48.        CASE WHEN ([Distinct1].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1],
  49.        [Limit4].[C4] AS [C2],
  50.        [Limit4].[SecurityLevel] AS [SecurityLevel],
  51.        [Limit4].[IsDriverEnabled] AS [IsDriverEnabled],
  52.        [Limit4].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  53.        [Limit4].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  54.        [Limit4].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  55.        [Limit4].[SendEmailsFlag] AS [SendEmailsFlag],
  56.        [Limit4].[AllowViewAcordXml] AS [AllowViewAcordXml],
  57.        [Limit4].[IsAppulateTeam] AS [IsAppulateTeam],
  58.        [Limit4].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  59.        [Limit4].[LoginLockingEnabled] AS [LoginLockingEnabled],
  60.        [Limit4].[UplinkSettings] AS [UplinkSettings],
  61.        [Limit4].[ShortName] AS [ShortName],
  62.        [Limit4].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  63.        [Limit4].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  64.        [Limit4].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  65.        [Limit4].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  66.        [Limit4].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  67.        [Limit4].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  68.        [Limit4].[Id1] AS [Id],
  69.        [Limit4].[Id2] AS [Id1],
  70.        [Limit4].[Id3] AS [Id2],
  71.        [Limit4].[MarketId] AS [MarketId],
  72.        [Limit4].[CompanyId] AS [CompanyId],
  73.        [Limit4].[MarketId1] AS [MarketId1],
  74.        [Limit4].[CompanyId1] AS [CompanyId1],
  75.        [Limit4].[C1] AS [C3],
  76.        [Limit4].[Id] AS [Id3],
  77.        [Limit4].[Name] AS [Name],
  78.        [Limit4].[DateRegistered] AS [DateRegistered],
  79.        [Limit4].[City] AS [City],
  80.        [Limit4].[StateId] AS [StateId],
  81.        [Limit4].[Phone] AS [Phone],
  82.        [Limit4].[C4] AS [C4],
  83.        [Limit4].[LastLoginTime] AS [LastLoginTime],
  84.        [Limit4].[C2] AS [C5],
  85.        [Limit4].[IsTesting] AS [IsTesting],
  86.        [Limit4].[C3] AS [C6],
  87.        [Distinct1].[Id] AS [Id4],
  88.        [Distinct1].[MarketId] AS [MarketId2],
  89.        [Distinct1].[Name] AS [Name1],
  90.        [Distinct1].[StateId] AS [StateId1],
  91.        [Distinct1].[NotificationEmail] AS [NotificationEmail],
  92.        CAST(NULL AS varchar(1)) AS [C7],
  93.        CAST(NULL AS varchar(1)) AS [C8]
  94.        FROM   (SELECT [Project11].[Id] AS [Id], [Project11].[Name] AS [Name], [Project11].[DateRegistered] AS [DateRegistered], [Project11].[IsTesting] AS [IsTesting], [Project11].[SecurityLevel] AS [SecurityLevel], [Project11].[IsDriverEnabled] AS [IsDriverEnabled], [Project11].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns], [Project11].[InterlocationSharingLevel] AS [InterlocationSharingLevel], [Project11].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification], [Project11].[SendEmailsFlag] AS [SendEmailsFlag], [Project11].[AllowViewAcordXml] AS [AllowViewAcordXml], [Project11].[IsAppulateTeam] AS [IsAppulateTeam], [Project11].[UplinkOcrEnabled] AS [UplinkOcrEnabled], [Project11].[LoginLockingEnabled] AS [LoginLockingEnabled], [Project11].[UplinkSettings] AS [UplinkSettings], [Project11].[ShortName] AS [ShortName], [Project11].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType], [Project11].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets], [Project11].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed], [Project11].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations], [Project11].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations], [Project11].[IsVisibleForMarkets] AS [IsVisibleForMarkets], [Project11].[Id1] AS [Id1], [Project11].[City] AS [City], [Project11].[StateId] AS [StateId], [Project11].[Phone] AS [Phone], [Project11].[Id2] AS [Id2], [Project11].[LastLoginTime] AS [LastLoginTime], [Project11].[Id3] AS [Id3], [Project11].[MarketId] AS [MarketId], [Project11].[CompanyId] AS [CompanyId], [Project11].[MarketId1] AS [MarketId1], [Project11].[CompanyId1] AS [CompanyId1], [Project11].[C1] AS [C1], [Project11].[C2] AS [C2], [Project11].[C3] AS [C3], [Project11].[C4] AS [C4]
  95.            FROM ( SELECT
  96.                @p__linq__2 AS [p__linq__2],
  97.                [Project10].[Id] AS [Id],
  98.                [Project10].[Name] AS [Name],
  99.                [Project10].[DateRegistered] AS [DateRegistered],
  100.                [Project10].[IsTesting] AS [IsTesting],
  101.                [Project10].[SecurityLevel] AS [SecurityLevel],
  102.                [Project10].[IsDriverEnabled] AS [IsDriverEnabled],
  103.                [Project10].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  104.                [Project10].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  105.                [Project10].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  106.                [Project10].[SendEmailsFlag] AS [SendEmailsFlag],
  107.                [Project10].[AllowViewAcordXml] AS [AllowViewAcordXml],
  108.                [Project10].[IsAppulateTeam] AS [IsAppulateTeam],
  109.                [Project10].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  110.                [Project10].[LoginLockingEnabled] AS [LoginLockingEnabled],
  111.                [Project10].[UplinkSettings] AS [UplinkSettings],
  112.                [Project10].[ShortName] AS [ShortName],
  113.                [Project10].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  114.                [Project10].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  115.                [Project10].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  116.                [Project10].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  117.                [Project10].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  118.                [Project10].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  119.                [Project10].[Id1] AS [Id1],
  120.                [Project10].[City] AS [City],
  121.                [Project10].[StateId] AS [StateId],
  122.                [Project10].[Phone] AS [Phone],
  123.                [Project10].[Id2] AS [Id2],
  124.                [Project10].[LastLoginTime] AS [LastLoginTime],
  125.                [Project10].[Id3] AS [Id3],
  126.                [Project10].[MarketId] AS [MarketId],
  127.                [Project10].[CompanyId] AS [CompanyId],
  128.                [Project10].[MarketId1] AS [MarketId1],
  129.                [Project10].[CompanyId1] AS [CompanyId1],
  130.                [Project10].[C1] AS [C1],
  131.                [Project10].[C2] AS [C2],
  132.                [Project10].[C3] AS [C3],
  133.                [Project10].[C4] AS [C4]
  134.                FROM ( SELECT
  135.                    @p__linq__2 AS [p__linq__2],
  136.                    [Project8].[Id] AS [Id],
  137.                    [Project8].[Name] AS [Name],
  138.                    [Project8].[DateRegistered] AS [DateRegistered],
  139.                    [Project8].[IsTesting] AS [IsTesting],
  140.                    [Project8].[SecurityLevel] AS [SecurityLevel],
  141.                    [Project8].[IsDriverEnabled] AS [IsDriverEnabled],
  142.                    [Project8].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  143.                    [Project8].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  144.                    [Project8].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  145.                    [Project8].[SendEmailsFlag] AS [SendEmailsFlag],
  146.                    [Project8].[AllowViewAcordXml] AS [AllowViewAcordXml],
  147.                    [Project8].[IsAppulateTeam] AS [IsAppulateTeam],
  148.                    [Project8].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  149.                    [Project8].[LoginLockingEnabled] AS [LoginLockingEnabled],
  150.                    [Project8].[UplinkSettings] AS [UplinkSettings],
  151.                    [Project8].[ShortName] AS [ShortName],
  152.                    [Project8].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  153.                    [Project8].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  154.                    [Project8].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  155.                    [Project8].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  156.                    [Project8].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  157.                    [Project8].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  158.                    [Project8].[Id1] AS [Id1],
  159.                    [Project8].[City] AS [City],
  160.                    [Project8].[StateId] AS [StateId],
  161.                    [Project8].[Phone] AS [Phone],
  162.                    [Project8].[Id2] AS [Id2],
  163.                    [Project8].[LastLoginTime] AS [LastLoginTime],
  164.                    [Project8].[Id3] AS [Id3],
  165.                    [Project8].[MarketId] AS [MarketId],
  166.                    [Project8].[CompanyId] AS [CompanyId],
  167.                    [Project8].[MarketId1] AS [MarketId1],
  168.                    [Project8].[CompanyId1] AS [CompanyId1],
  169.                    1 AS [C1],
  170.                    CASE WHEN ( EXISTS (SELECT
  171.                        1 AS [C1]
  172.                        FROM [dbo].[CompanyToMarket] AS [Extent11]
  173.                        WHERE ([Extent11].[MarketId] = @p__linq__4) AND ([Extent11].[CompanyId] = [Project8].[Id]) AND (0 = [Extent11].[Disabled]) AND (1 = [Extent11].[MarketAccessType])
  174.                    )) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C2],
  175.                    CASE WHEN ([Project8].[AgencyAppointmentStatus] IS NULL) THEN 0 ELSE [Project8].[AgencyAppointmentStatus1] END AS [C3],
  176.                    [Project8].[C1] AS [C4]
  177.                    FROM ( SELECT
  178.                        [Project7].[Id] AS [Id],
  179.                        [Project7].[Name] AS [Name],
  180.                        [Project7].[DateRegistered] AS [DateRegistered],
  181.                        [Project7].[IsTesting] AS [IsTesting],
  182.                        [Project7].[SecurityLevel] AS [SecurityLevel],
  183.                        [Project7].[IsDriverEnabled] AS [IsDriverEnabled],
  184.                        [Project7].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  185.                        [Project7].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  186.                        [Project7].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  187.                        [Project7].[SendEmailsFlag] AS [SendEmailsFlag],
  188.                        [Project7].[AllowViewAcordXml] AS [AllowViewAcordXml],
  189.                        [Project7].[IsAppulateTeam] AS [IsAppulateTeam],
  190.                        [Project7].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  191.                        [Project7].[LoginLockingEnabled] AS [LoginLockingEnabled],
  192.                        [Project7].[UplinkSettings] AS [UplinkSettings],
  193.                        [Project7].[ShortName] AS [ShortName],
  194.                        [Project7].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  195.                        [Project7].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  196.                        [Project7].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  197.                        [Project7].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  198.                        [Project7].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  199.                        [Project7].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  200.                        [Project7].[Id1] AS [Id1],
  201.                        [Project7].[City] AS [City],
  202.                        [Project7].[StateId] AS [StateId],
  203.                        [Project7].[Phone] AS [Phone],
  204.                        [Project7].[Id2] AS [Id2],
  205.                        [Project7].[LastLoginTime] AS [LastLoginTime],
  206.                        [Project7].[Id3] AS [Id3],
  207.                        [Project7].[MarketId] AS [MarketId],
  208.                        [Project7].[CompanyId] AS [CompanyId],
  209.                        [Project7].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus],
  210.                        [Project7].[MarketId1] AS [MarketId1],
  211.                        [Project7].[CompanyId1] AS [CompanyId1],
  212.                        [Project7].[AgencyAppointmentStatus1] AS [AgencyAppointmentStatus1],
  213.                        (SELECT
  214.                            COUNT(1) AS [A1]
  215.                            FROM   (SELECT [Extent8].[MarketId] AS [MarketId], [Extent9].[LocationId] AS [LocationId]
  216.                                FROM  [dbo].[SubmissionPackage] AS [Extent8]
  217.                                INNER JOIN [dbo].[User] AS [Extent9] ON [Extent8].[CreatorId] = [Extent9].[Id]
  218.                                WHERE (0 <> [Extent8].[Status]) AND (3 <> [Extent8].[Status]) ) AS [Filter6]
  219.                            INNER JOIN [dbo].[Location] AS [Extent10] ON [Filter6].[LocationId] = [Extent10].[Id]
  220.                            WHERE ([Filter6].[MarketId] = @p__linq__3) AND ([Extent10].[CompanyId] = [Project7].[Id])) AS [C1]
  221.                        FROM ( SELECT
  222.                            [Project5].[Id] AS [Id],
  223.                            [Project5].[Name] AS [Name],
  224.                            [Project5].[DateRegistered] AS [DateRegistered],
  225.                            [Project5].[IsTesting] AS [IsTesting],
  226.                            [Project5].[SecurityLevel] AS [SecurityLevel],
  227.                            [Project5].[IsDriverEnabled] AS [IsDriverEnabled],
  228.                            [Project5].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  229.                            [Project5].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  230.                            [Project5].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  231.                            [Project5].[SendEmailsFlag] AS [SendEmailsFlag],
  232.                            [Project5].[AllowViewAcordXml] AS [AllowViewAcordXml],
  233.                            [Project5].[IsAppulateTeam] AS [IsAppulateTeam],
  234.                            [Project5].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  235.                            [Project5].[LoginLockingEnabled] AS [LoginLockingEnabled],
  236.                            [Project5].[UplinkSettings] AS [UplinkSettings],
  237.                            [Project5].[ShortName] AS [ShortName],
  238.                            [Project5].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  239.                            [Project5].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  240.                            [Project5].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  241.                            [Project5].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  242.                            [Project5].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  243.                            [Project5].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  244.                            [Project5].[Id1] AS [Id1],
  245.                            [Project5].[City] AS [City],
  246.                            [Project5].[StateId] AS [StateId],
  247.                            [Project5].[Phone] AS [Phone],
  248.                            [Project5].[Id2] AS [Id2],
  249.                            [Project5].[LastLoginTime] AS [LastLoginTime],
  250.                            [Project5].[Id3] AS [Id3],
  251.                            [Project5].[MarketId] AS [MarketId],
  252.                            [Project5].[CompanyId] AS [CompanyId],
  253.                            [Project5].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus],
  254.                            [Limit3].[MarketId] AS [MarketId1],
  255.                            [Limit3].[CompanyId] AS [CompanyId1],
  256.                            [Limit3].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus1]
  257.                            FROM   (SELECT
  258.                                [Project3].[Id] AS [Id],
  259.                                [Project3].[Name] AS [Name],
  260.                                [Project3].[DateRegistered] AS [DateRegistered],
  261.                                [Project3].[IsTesting] AS [IsTesting],
  262.                                [Project3].[SecurityLevel] AS [SecurityLevel],
  263.                                [Project3].[IsDriverEnabled] AS [IsDriverEnabled],
  264.                                [Project3].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  265.                                [Project3].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  266.                                [Project3].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  267.                                [Project3].[SendEmailsFlag] AS [SendEmailsFlag],
  268.                                [Project3].[AllowViewAcordXml] AS [AllowViewAcordXml],
  269.                                [Project3].[IsAppulateTeam] AS [IsAppulateTeam],
  270.                                [Project3].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  271.                                [Project3].[LoginLockingEnabled] AS [LoginLockingEnabled],
  272.                                [Project3].[UplinkSettings] AS [UplinkSettings],
  273.                                [Project3].[ShortName] AS [ShortName],
  274.                                [Project3].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  275.                                [Project3].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  276.                                [Project3].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  277.                                [Project3].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  278.                                [Project3].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  279.                                [Project3].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  280.                                [Project3].[Id1] AS [Id1],
  281.                                [Project3].[City] AS [City],
  282.                                [Project3].[StateId] AS [StateId],
  283.                                [Project3].[Phone] AS [Phone],
  284.                                [Project3].[Id2] AS [Id2],
  285.                                [Project3].[LastLoginTime] AS [LastLoginTime],
  286.                                [Project3].[Id3] AS [Id3],
  287.                                [Limit2].[MarketId] AS [MarketId],
  288.                                [Limit2].[CompanyId] AS [CompanyId],
  289.                                [Limit2].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  290.                                FROM   (SELECT
  291.                                    [Filter2].[Id4] AS [Id],
  292.                                    [Filter2].[Name1] AS [Name],
  293.                                    [Filter2].[DateRegistered] AS [DateRegistered],
  294.                                    [Filter2].[IsTesting] AS [IsTesting],
  295.                                    [Filter2].[SecurityLevel] AS [SecurityLevel],
  296.                                    [Filter2].[IsDriverEnabled] AS [IsDriverEnabled],
  297.                                    [Filter2].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  298.                                    [Filter2].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  299.                                    [Filter2].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  300.                                    [Filter2].[SendEmailsFlag] AS [SendEmailsFlag],
  301.                                    [Filter2].[AllowViewAcordXml] AS [AllowViewAcordXml],
  302.                                    [Filter2].[IsAppulateTeam] AS [IsAppulateTeam],
  303.                                    [Filter2].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  304.                                    [Filter2].[LoginLockingEnabled] AS [LoginLockingEnabled],
  305.                                    [Filter2].[UplinkSettings] AS [UplinkSettings],
  306.                                    [Filter2].[ShortName] AS [ShortName],
  307.                                    [Filter2].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  308.                                    [Filter2].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  309.                                    [Filter2].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  310.                                    [Filter2].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  311.                                    [Filter2].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  312.                                    [Filter2].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  313.                                    [Filter2].[Id5] AS [Id1],
  314.                                    [Filter2].[City] AS [City],
  315.                                    [Filter2].[StateId] AS [StateId],
  316.                                    [Filter2].[Phone] AS [Phone],
  317.                                    [Limit1].[Id] AS [Id2],
  318.                                    [Limit1].[LastLoginTime] AS [LastLoginTime],
  319.                                    [Limit1].[Id1] AS [Id3]
  320.                                    FROM   (SELECT [Extent1].[Id] AS [Id4], [Extent1].[Name] AS [Name1], [Extent1].[DateRegistered] AS [DateRegistered], [Extent1].[IsTesting] AS [IsTesting], [Extent1].[SecurityLevel] AS [SecurityLevel], [Extent1].[IsDriverEnabled] AS [IsDriverEnabled], [Extent1].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns], [Extent1].[InterlocationSharingLevel] AS [InterlocationSharingLevel], [Extent1].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification], [Extent1].[SendEmailsFlag] AS [SendEmailsFlag], [Extent1].[AllowViewAcordXml] AS [AllowViewAcordXml], [Extent1].[IsAppulateTeam] AS [IsAppulateTeam], [Extent1].[UplinkOcrEnabled] AS [UplinkOcrEnabled], [Extent1].[LoginLockingEnabled] AS [LoginLockingEnabled], [Extent1].[UplinkSettings] AS [UplinkSettings], [Extent1].[ShortName] AS [ShortName], [Extent1].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType], [Extent1].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets], [Extent1].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed], [Extent1].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations], [Extent1].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations], [Extent1].[IsVisibleForMarkets] AS [IsVisibleForMarkets], [Extent2].[Id] AS [Id5], [Extent2].[City] AS [City], [Extent2].[StateId] AS [StateId], [Extent2].[Phone] AS [Phone]
  321.                                        FROM  [dbo].[CompanyFlatView] AS [Extent1]
  322.                                        INNER JOIN [dbo].[Location] AS [Extent2] ON [Extent1].[Id] = [Extent2].[CompanyId]
  323.                                        WHERE ([Extent1].[Id] <> @p__linq__0) AND ([Extent1].[DisableDate] IS NULL) AND ([Extent2].[IsPrimaryAddress] = 1) AND ( EXISTS (SELECT
  324.                                            1 AS [C1]
  325.                                            FROM [dbo].[CompanyToMarket] AS [Extent3]
  326.                                            WHERE (1 = [Extent3].[MarketAccessType]) AND ([Extent3].[MarketId] = @p__linq__1) AND ([Extent3].[CompanyId] = [Extent1].[Id])
  327.                                        )) ) AS [Filter2]
  328.                                    OUTER APPLY  (SELECT TOP (1) [Project2].[Id] AS [Id], [Project2].[LastLoginTime] AS [LastLoginTime], [Project2].[Id1] AS [Id1]
  329.                                        FROM ( SELECT
  330.                                            [Extent4].[Id] AS [Id],
  331.                                            [Extent4].[LastLoginTime] AS [LastLoginTime],
  332.                                            [Extent5].[Id] AS [Id1]
  333.                                            FROM  [dbo].[User] AS [Extent4]
  334.                                            INNER JOIN [dbo].[Location] AS [Extent5] ON [Extent4].[LocationId] = [Extent5].[Id]
  335.                                            WHERE [Extent5].[CompanyId] = [Filter2].[Id4]
  336.                                        )  AS [Project2]
  337.                                        ORDER BY [Project2].[LastLoginTime] DESC ) AS [Limit1] ) AS [Project3]
  338.                                OUTER APPLY  (SELECT TOP (1)
  339.                                    [Extent6].[MarketId] AS [MarketId],
  340.                                    [Extent6].[CompanyId] AS [CompanyId],
  341.                                    [Extent6].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  342.                                    FROM [dbo].[CompanyToMarket] AS [Extent6]
  343.                                    WHERE ([Extent6].[MarketId] = @p__linq__7) AND ([Extent6].[CompanyId] = [Project3].[Id]) ) AS [Limit2] ) AS [Project5]
  344.                            OUTER APPLY  (SELECT TOP (1)
  345.                                [Extent7].[MarketId] AS [MarketId],
  346.                                [Extent7].[CompanyId] AS [CompanyId],
  347.                                [Extent7].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  348.                                FROM [dbo].[CompanyToMarket] AS [Extent7]
  349.                                WHERE ([Extent7].[MarketId] = @p__linq__7) AND ([Extent7].[CompanyId] = [Project5].[Id]) ) AS [Limit3]
  350.                        )  AS [Project7]
  351.                    )  AS [Project8]
  352.                )  AS [Project10]
  353.            )  AS [Project11]
  354.            ORDER BY [Project11].[C4] ASC
  355.            OFFSET 20 ROWS FETCH NEXT 20 ROWS ONLY  ) AS [Limit4]
  356.        OUTER APPLY  (SELECT DISTINCT
  357.            [Join5].[Id] AS [Id],
  358.            [Join5].[MarketId] AS [MarketId],
  359.            [Join5].[Name] AS [Name],
  360.            [Join5].[StateId] AS [StateId],
  361.            [Join5].[NotificationEmail] AS [NotificationEmail]
  362.            FROM  [dbo].[Location] AS [Extent12]
  363.            INNER JOIN  (SELECT [Extent13].[LocationId] AS [LocationId], [Extent14].[Id] AS [Id], [Extent14].[MarketId] AS [MarketId], [Extent14].[Name] AS [Name], [Extent14].[StateId] AS [StateId], [Extent14].[NotificationEmail] AS [NotificationEmail]
  364.                FROM  [dbo].[LocationToRegion] AS [Extent13]
  365.                INNER JOIN [dbo].[Region] AS [Extent14] ON [Extent14].[Id] = [Extent13].[RegionId] ) AS [Join5] ON [Extent12].[Id] = [Join5].[LocationId]
  366.            WHERE ([Extent12].[IsDeleted] <> 1) AND ([Join5].[MarketId] = @p__linq__2) AND ([Extent12].[CompanyId] = [Limit4].[Id]) ) AS [Distinct1]
  367.    UNION ALL
  368.        SELECT
  369.        2 AS [C1],
  370.        [Limit8].[C3] AS [C2],
  371.        [Limit8].[SecurityLevel] AS [SecurityLevel],
  372.        [Limit8].[IsDriverEnabled] AS [IsDriverEnabled],
  373.        [Limit8].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  374.        [Limit8].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  375.        [Limit8].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  376.        [Limit8].[SendEmailsFlag] AS [SendEmailsFlag],
  377.        [Limit8].[AllowViewAcordXml] AS [AllowViewAcordXml],
  378.        [Limit8].[IsAppulateTeam] AS [IsAppulateTeam],
  379.        [Limit8].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  380.        [Limit8].[LoginLockingEnabled] AS [LoginLockingEnabled],
  381.        [Limit8].[UplinkSettings] AS [UplinkSettings],
  382.        [Limit8].[ShortName] AS [ShortName],
  383.        [Limit8].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  384.        [Limit8].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  385.        [Limit8].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  386.        [Limit8].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  387.        [Limit8].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  388.        [Limit8].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  389.        [Limit8].[Id1] AS [Id],
  390.        [Limit8].[Id2] AS [Id1],
  391.        [Limit8].[Id3] AS [Id2],
  392.        [Limit8].[MarketId] AS [MarketId],
  393.        [Limit8].[CompanyId] AS [CompanyId],
  394.        [Limit8].[MarketId1] AS [MarketId1],
  395.        [Limit8].[CompanyId1] AS [CompanyId1],
  396.        [Limit8].[Id] AS [Id3],
  397.        [Limit8].[Id] AS [Id4],
  398.        [Limit8].[Name] AS [Name],
  399.        [Limit8].[DateRegistered] AS [DateRegistered],
  400.        [Limit8].[City] AS [City],
  401.        [Limit8].[StateId] AS [StateId],
  402.        [Limit8].[Phone] AS [Phone],
  403.        [Limit8].[C3] AS [C3],
  404.        [Limit8].[LastLoginTime] AS [LastLoginTime],
  405.        [Limit8].[C1] AS [C4],
  406.        [Limit8].[IsTesting] AS [IsTesting],
  407.        [Limit8].[C2] AS [C5],
  408.        CAST(NULL AS int) AS [C6],
  409.        CAST(NULL AS int) AS [C7],
  410.        CAST(NULL AS varchar(1)) AS [C8],
  411.        CAST(NULL AS varchar(1)) AS [C9],
  412.        CAST(NULL AS varchar(1)) AS [C10],
  413.        [Extent26].[Code] AS [Code],
  414.        CAST(NULL AS varchar(1)) AS [C11]
  415.        FROM   (SELECT [Project24].[Id] AS [Id], [Project24].[Name] AS [Name], [Project24].[DateRegistered] AS [DateRegistered], [Project24].[IsTesting] AS [IsTesting], [Project24].[SecurityLevel] AS [SecurityLevel], [Project24].[IsDriverEnabled] AS [IsDriverEnabled], [Project24].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns], [Project24].[InterlocationSharingLevel] AS [InterlocationSharingLevel], [Project24].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification], [Project24].[SendEmailsFlag] AS [SendEmailsFlag], [Project24].[AllowViewAcordXml] AS [AllowViewAcordXml], [Project24].[IsAppulateTeam] AS [IsAppulateTeam], [Project24].[UplinkOcrEnabled] AS [UplinkOcrEnabled], [Project24].[LoginLockingEnabled] AS [LoginLockingEnabled], [Project24].[UplinkSettings] AS [UplinkSettings], [Project24].[ShortName] AS [ShortName], [Project24].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType], [Project24].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets], [Project24].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed], [Project24].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations], [Project24].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations], [Project24].[IsVisibleForMarkets] AS [IsVisibleForMarkets], [Project24].[Id1] AS [Id1], [Project24].[City] AS [City], [Project24].[StateId] AS [StateId], [Project24].[Phone] AS [Phone], [Project24].[Id2] AS [Id2], [Project24].[LastLoginTime] AS [LastLoginTime], [Project24].[Id3] AS [Id3], [Project24].[MarketId] AS [MarketId], [Project24].[CompanyId] AS [CompanyId], [Project24].[MarketId1] AS [MarketId1], [Project24].[CompanyId1] AS [CompanyId1], [Project24].[C1] AS [C1], [Project24].[C2] AS [C2], [Project24].[C3] AS [C3]
  416.            FROM ( SELECT
  417.                @p__linq__2 AS [p__linq__2],
  418.                @p__linq__5 AS [p__linq__5],
  419.                [Project23].[Id] AS [Id],
  420.                [Project23].[Name] AS [Name],
  421.                [Project23].[DateRegistered] AS [DateRegistered],
  422.                [Project23].[IsTesting] AS [IsTesting],
  423.                [Project23].[SecurityLevel] AS [SecurityLevel],
  424.                [Project23].[IsDriverEnabled] AS [IsDriverEnabled],
  425.                [Project23].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  426.                [Project23].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  427.                [Project23].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  428.                [Project23].[SendEmailsFlag] AS [SendEmailsFlag],
  429.                [Project23].[AllowViewAcordXml] AS [AllowViewAcordXml],
  430.                [Project23].[IsAppulateTeam] AS [IsAppulateTeam],
  431.                [Project23].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  432.                [Project23].[LoginLockingEnabled] AS [LoginLockingEnabled],
  433.                [Project23].[UplinkSettings] AS [UplinkSettings],
  434.                [Project23].[ShortName] AS [ShortName],
  435.                [Project23].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  436.                [Project23].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  437.                [Project23].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  438.                [Project23].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  439.                [Project23].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  440.                [Project23].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  441.                [Project23].[Id1] AS [Id1],
  442.                [Project23].[City] AS [City],
  443.                [Project23].[StateId] AS [StateId],
  444.                [Project23].[Phone] AS [Phone],
  445.                [Project23].[Id2] AS [Id2],
  446.                [Project23].[LastLoginTime] AS [LastLoginTime],
  447.                [Project23].[Id3] AS [Id3],
  448.                [Project23].[MarketId] AS [MarketId],
  449.                [Project23].[CompanyId] AS [CompanyId],
  450.                [Project23].[MarketId1] AS [MarketId1],
  451.                [Project23].[CompanyId1] AS [CompanyId1],
  452.                [Project23].[C1] AS [C1],
  453.                [Project23].[C2] AS [C2],
  454.                [Project23].[C3] AS [C3]
  455.                FROM ( SELECT
  456.                    @p__linq__2 AS [p__linq__2],
  457.                    @p__linq__5 AS [p__linq__5],
  458.                    [Project21].[Id] AS [Id],
  459.                    [Project21].[Name] AS [Name],
  460.                    [Project21].[DateRegistered] AS [DateRegistered],
  461.                    [Project21].[IsTesting] AS [IsTesting],
  462.                    [Project21].[SecurityLevel] AS [SecurityLevel],
  463.                    [Project21].[IsDriverEnabled] AS [IsDriverEnabled],
  464.                    [Project21].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  465.                    [Project21].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  466.                    [Project21].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  467.                    [Project21].[SendEmailsFlag] AS [SendEmailsFlag],
  468.                    [Project21].[AllowViewAcordXml] AS [AllowViewAcordXml],
  469.                    [Project21].[IsAppulateTeam] AS [IsAppulateTeam],
  470.                    [Project21].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  471.                    [Project21].[LoginLockingEnabled] AS [LoginLockingEnabled],
  472.                    [Project21].[UplinkSettings] AS [UplinkSettings],
  473.                    [Project21].[ShortName] AS [ShortName],
  474.                    [Project21].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  475.                    [Project21].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  476.                    [Project21].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  477.                    [Project21].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  478.                    [Project21].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  479.                    [Project21].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  480.                    [Project21].[Id1] AS [Id1],
  481.                    [Project21].[City] AS [City],
  482.                    [Project21].[StateId] AS [StateId],
  483.                    [Project21].[Phone] AS [Phone],
  484.                    [Project21].[Id2] AS [Id2],
  485.                    [Project21].[LastLoginTime] AS [LastLoginTime],
  486.                    [Project21].[Id3] AS [Id3],
  487.                    [Project21].[MarketId] AS [MarketId],
  488.                    [Project21].[CompanyId] AS [CompanyId],
  489.                    [Project21].[MarketId1] AS [MarketId1],
  490.                    [Project21].[CompanyId1] AS [CompanyId1],
  491.                    CASE WHEN ( EXISTS (SELECT
  492.                        1 AS [C1]
  493.                        FROM [dbo].[CompanyToMarket] AS [Extent25]
  494.                        WHERE ([Extent25].[MarketId] = @p__linq__4) AND ([Extent25].[CompanyId] = [Project21].[Id]) AND (0 = [Extent25].[Disabled]) AND (1 = [Extent25].[MarketAccessType])
  495.                    )) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C1],
  496.                    CASE WHEN ([Project21].[AgencyAppointmentStatus] IS NULL) THEN 0 ELSE [Project21].[AgencyAppointmentStatus1] END AS [C2],
  497.                    [Project21].[C1] AS [C3]
  498.                    FROM ( SELECT
  499.                        [Project20].[Id] AS [Id],
  500.                        [Project20].[Name] AS [Name],
  501.                        [Project20].[DateRegistered] AS [DateRegistered],
  502.                        [Project20].[IsTesting] AS [IsTesting],
  503.                        [Project20].[SecurityLevel] AS [SecurityLevel],
  504.                        [Project20].[IsDriverEnabled] AS [IsDriverEnabled],
  505.                        [Project20].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  506.                        [Project20].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  507.                        [Project20].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  508.                        [Project20].[SendEmailsFlag] AS [SendEmailsFlag],
  509.                        [Project20].[AllowViewAcordXml] AS [AllowViewAcordXml],
  510.                        [Project20].[IsAppulateTeam] AS [IsAppulateTeam],
  511.                        [Project20].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  512.                        [Project20].[LoginLockingEnabled] AS [LoginLockingEnabled],
  513.                        [Project20].[UplinkSettings] AS [UplinkSettings],
  514.                        [Project20].[ShortName] AS [ShortName],
  515.                        [Project20].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  516.                        [Project20].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  517.                        [Project20].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  518.                        [Project20].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  519.                        [Project20].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  520.                        [Project20].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  521.                        [Project20].[Id1] AS [Id1],
  522.                        [Project20].[City] AS [City],
  523.                        [Project20].[StateId] AS [StateId],
  524.                        [Project20].[Phone] AS [Phone],
  525.                        [Project20].[Id2] AS [Id2],
  526.                        [Project20].[LastLoginTime] AS [LastLoginTime],
  527.                        [Project20].[Id3] AS [Id3],
  528.                        [Project20].[MarketId] AS [MarketId],
  529.                        [Project20].[CompanyId] AS [CompanyId],
  530.                        [Project20].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus],
  531.                        [Project20].[MarketId1] AS [MarketId1],
  532.                        [Project20].[CompanyId1] AS [CompanyId1],
  533.                        [Project20].[AgencyAppointmentStatus1] AS [AgencyAppointmentStatus1],
  534.                        (SELECT
  535.                            COUNT(1) AS [A1]
  536.                            FROM   (SELECT [Extent22].[MarketId] AS [MarketId], [Extent23].[LocationId] AS [LocationId]
  537.                                FROM  [dbo].[SubmissionPackage] AS [Extent22]
  538.                                INNER JOIN [dbo].[User] AS [Extent23] ON [Extent22].[CreatorId] = [Extent23].[Id]
  539.                                WHERE (0 <> [Extent22].[Status]) AND (3 <> [Extent22].[Status]) ) AS [Filter15]
  540.                            INNER JOIN [dbo].[Location] AS [Extent24] ON [Filter15].[LocationId] = [Extent24].[Id]
  541.                            WHERE ([Filter15].[MarketId] = @p__linq__3) AND ([Extent24].[CompanyId] = [Project20].[Id])) AS [C1]
  542.                        FROM ( SELECT
  543.                            [Project18].[Id] AS [Id],
  544.                            [Project18].[Name] AS [Name],
  545.                            [Project18].[DateRegistered] AS [DateRegistered],
  546.                            [Project18].[IsTesting] AS [IsTesting],
  547.                            [Project18].[SecurityLevel] AS [SecurityLevel],
  548.                            [Project18].[IsDriverEnabled] AS [IsDriverEnabled],
  549.                            [Project18].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  550.                            [Project18].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  551.                            [Project18].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  552.                            [Project18].[SendEmailsFlag] AS [SendEmailsFlag],
  553.                            [Project18].[AllowViewAcordXml] AS [AllowViewAcordXml],
  554.                            [Project18].[IsAppulateTeam] AS [IsAppulateTeam],
  555.                            [Project18].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  556.                            [Project18].[LoginLockingEnabled] AS [LoginLockingEnabled],
  557.                            [Project18].[UplinkSettings] AS [UplinkSettings],
  558.                            [Project18].[ShortName] AS [ShortName],
  559.                            [Project18].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  560.                            [Project18].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  561.                            [Project18].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  562.                            [Project18].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  563.                            [Project18].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  564.                            [Project18].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  565.                            [Project18].[Id1] AS [Id1],
  566.                            [Project18].[City] AS [City],
  567.                            [Project18].[StateId] AS [StateId],
  568.                            [Project18].[Phone] AS [Phone],
  569.                            [Project18].[Id2] AS [Id2],
  570.                            [Project18].[LastLoginTime] AS [LastLoginTime],
  571.                            [Project18].[Id3] AS [Id3],
  572.                            [Project18].[MarketId] AS [MarketId],
  573.                            [Project18].[CompanyId] AS [CompanyId],
  574.                            [Project18].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus],
  575.                            [Limit7].[MarketId] AS [MarketId1],
  576.                            [Limit7].[CompanyId] AS [CompanyId1],
  577.                            [Limit7].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus1]
  578.                            FROM   (SELECT
  579.                                [Project16].[Id] AS [Id],
  580.                                [Project16].[Name] AS [Name],
  581.                                [Project16].[DateRegistered] AS [DateRegistered],
  582.                                [Project16].[IsTesting] AS [IsTesting],
  583.                                [Project16].[SecurityLevel] AS [SecurityLevel],
  584.                                [Project16].[IsDriverEnabled] AS [IsDriverEnabled],
  585.                                [Project16].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  586.                                [Project16].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  587.                                [Project16].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  588.                                [Project16].[SendEmailsFlag] AS [SendEmailsFlag],
  589.                                [Project16].[AllowViewAcordXml] AS [AllowViewAcordXml],
  590.                                [Project16].[IsAppulateTeam] AS [IsAppulateTeam],
  591.                                [Project16].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  592.                                [Project16].[LoginLockingEnabled] AS [LoginLockingEnabled],
  593.                                [Project16].[UplinkSettings] AS [UplinkSettings],
  594.                                [Project16].[ShortName] AS [ShortName],
  595.                                [Project16].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  596.                                [Project16].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  597.                                [Project16].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  598.                                [Project16].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  599.                                [Project16].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  600.                                [Project16].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  601.                                [Project16].[Id1] AS [Id1],
  602.                                [Project16].[City] AS [City],
  603.                                [Project16].[StateId] AS [StateId],
  604.                                [Project16].[Phone] AS [Phone],
  605.                                [Project16].[Id2] AS [Id2],
  606.                                [Project16].[LastLoginTime] AS [LastLoginTime],
  607.                                [Project16].[Id3] AS [Id3],
  608.                                [Limit6].[MarketId] AS [MarketId],
  609.                                [Limit6].[CompanyId] AS [CompanyId],
  610.                                [Limit6].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  611.                                FROM   (SELECT
  612.                                    [Filter11].[Id6] AS [Id],
  613.                                    [Filter11].[Name2] AS [Name],
  614.                                    [Filter11].[DateRegistered] AS [DateRegistered],
  615.                                    [Filter11].[IsTesting] AS [IsTesting],
  616.                                    [Filter11].[SecurityLevel] AS [SecurityLevel],
  617.                                    [Filter11].[IsDriverEnabled] AS [IsDriverEnabled],
  618.                                    [Filter11].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  619.                                    [Filter11].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  620.                                    [Filter11].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  621.                                    [Filter11].[SendEmailsFlag] AS [SendEmailsFlag],
  622.                                    [Filter11].[AllowViewAcordXml] AS [AllowViewAcordXml],
  623.                                    [Filter11].[IsAppulateTeam] AS [IsAppulateTeam],
  624.                                    [Filter11].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  625.                                    [Filter11].[LoginLockingEnabled] AS [LoginLockingEnabled],
  626.                                    [Filter11].[UplinkSettings] AS [UplinkSettings],
  627.                                    [Filter11].[ShortName] AS [ShortName],
  628.                                    [Filter11].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  629.                                    [Filter11].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  630.                                    [Filter11].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  631.                                    [Filter11].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  632.                                    [Filter11].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  633.                                    [Filter11].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  634.                                    [Filter11].[Id7] AS [Id1],
  635.                                    [Filter11].[City] AS [City],
  636.                                    [Filter11].[StateId] AS [StateId],
  637.                                    [Filter11].[Phone] AS [Phone],
  638.                                    [Limit5].[Id] AS [Id2],
  639.                                    [Limit5].[LastLoginTime] AS [LastLoginTime],
  640.                                    [Limit5].[Id1] AS [Id3]
  641.                                    FROM   (SELECT [Extent15].[Id] AS [Id6], [Extent15].[Name] AS [Name2], [Extent15].[DateRegistered] AS [DateRegistered], [Extent15].[IsTesting] AS [IsTesting], [Extent15].[SecurityLevel] AS [SecurityLevel], [Extent15].[IsDriverEnabled] AS [IsDriverEnabled], [Extent15].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns], [Extent15].[InterlocationSharingLevel] AS [InterlocationSharingLevel], [Extent15].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification], [Extent15].[SendEmailsFlag] AS [SendEmailsFlag], [Extent15].[AllowViewAcordXml] AS [AllowViewAcordXml], [Extent15].[IsAppulateTeam] AS [IsAppulateTeam], [Extent15].[UplinkOcrEnabled] AS [UplinkOcrEnabled], [Extent15].[LoginLockingEnabled] AS [LoginLockingEnabled], [Extent15].[UplinkSettings] AS [UplinkSettings], [Extent15].[ShortName] AS [ShortName], [Extent15].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType], [Extent15].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets], [Extent15].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed], [Extent15].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations], [Extent15].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations], [Extent15].[IsVisibleForMarkets] AS [IsVisibleForMarkets], [Extent16].[Id] AS [Id7], [Extent16].[City] AS [City], [Extent16].[StateId] AS [StateId], [Extent16].[Phone] AS [Phone]
  642.                                        FROM  [dbo].[CompanyFlatView] AS [Extent15]
  643.                                        INNER JOIN [dbo].[Location] AS [Extent16] ON [Extent15].[Id] = [Extent16].[CompanyId]
  644.                                        WHERE ([Extent15].[Id] <> @p__linq__0) AND ([Extent15].[DisableDate] IS NULL) AND ([Extent16].[IsPrimaryAddress] = 1) AND ( EXISTS (SELECT
  645.                                            1 AS [C1]
  646.                                            FROM [dbo].[CompanyToMarket] AS [Extent17]
  647.                                            WHERE (1 = [Extent17].[MarketAccessType]) AND ([Extent17].[MarketId] = @p__linq__1) AND ([Extent17].[CompanyId] = [Extent15].[Id])
  648.                                        )) ) AS [Filter11]
  649.                                    OUTER APPLY  (SELECT TOP (1) [Project15].[Id] AS [Id], [Project15].[LastLoginTime] AS [LastLoginTime], [Project15].[Id1] AS [Id1]
  650.                                        FROM ( SELECT
  651.                                            [Extent18].[Id] AS [Id],
  652.                                            [Extent18].[LastLoginTime] AS [LastLoginTime],
  653.                                            [Extent19].[Id] AS [Id1]
  654.                                            FROM  [dbo].[User] AS [Extent18]
  655.                                            INNER JOIN [dbo].[Location] AS [Extent19] ON [Extent18].[LocationId] = [Extent19].[Id]
  656.                                            WHERE [Extent19].[CompanyId] = [Filter11].[Id6]
  657.                                        )  AS [Project15]
  658.                                        ORDER BY [Project15].[LastLoginTime] DESC ) AS [Limit5] ) AS [Project16]
  659.                                OUTER APPLY  (SELECT TOP (1)
  660.                                    [Extent20].[MarketId] AS [MarketId],
  661.                                    [Extent20].[CompanyId] AS [CompanyId],
  662.                                    [Extent20].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  663.                                    FROM [dbo].[CompanyToMarket] AS [Extent20]
  664.                                    WHERE ([Extent20].[MarketId] = @p__linq__7) AND ([Extent20].[CompanyId] = [Project16].[Id]) ) AS [Limit6] ) AS [Project18]
  665.                            OUTER APPLY  (SELECT TOP (1)
  666.                                [Extent21].[MarketId] AS [MarketId],
  667.                                [Extent21].[CompanyId] AS [CompanyId],
  668.                                [Extent21].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  669.                                FROM [dbo].[CompanyToMarket] AS [Extent21]
  670.                                WHERE ([Extent21].[MarketId] = @p__linq__7) AND ([Extent21].[CompanyId] = [Project18].[Id]) ) AS [Limit7]
  671.                        )  AS [Project20]
  672.                    )  AS [Project21]
  673.                )  AS [Project23]
  674.            )  AS [Project24]
  675.            ORDER BY [Project24].[C3] ASC
  676.            OFFSET 20 ROWS FETCH NEXT 20 ROWS ONLY  ) AS [Limit8]
  677.        INNER JOIN [dbo].[CompanyCode] AS [Extent26] ON ([Extent26].[MarketId] = @p__linq__5) AND ((LEN([Extent26].[Code])) > 0) AND ((([Extent26].[CompanyId] IS NULL) AND ( EXISTS (SELECT
  678.            1 AS [C1]
  679.            FROM [dbo].[Location] AS [Extent27]
  680.            WHERE ([Extent27].[Id] = [Extent26].[LocationId]) AND ([Extent27].[CompanyId] = [Limit8].[Id]) AND (0 = [Extent27].[IsDeleted])
  681.        ))) OR ([Extent26].[CompanyId] = [Limit8].[Id]))
  682.    UNION ALL
  683.        SELECT
  684.        3 AS [C1],
  685.        [Limit12].[C3] AS [C2],
  686.        [Limit12].[SecurityLevel] AS [SecurityLevel],
  687.        [Limit12].[IsDriverEnabled] AS [IsDriverEnabled],
  688.        [Limit12].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  689.        [Limit12].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  690.        [Limit12].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  691.        [Limit12].[SendEmailsFlag] AS [SendEmailsFlag],
  692.        [Limit12].[AllowViewAcordXml] AS [AllowViewAcordXml],
  693.        [Limit12].[IsAppulateTeam] AS [IsAppulateTeam],
  694.        [Limit12].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  695.        [Limit12].[LoginLockingEnabled] AS [LoginLockingEnabled],
  696.        [Limit12].[UplinkSettings] AS [UplinkSettings],
  697.        [Limit12].[ShortName] AS [ShortName],
  698.        [Limit12].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  699.        [Limit12].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  700.        [Limit12].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  701.        [Limit12].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  702.        [Limit12].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  703.        [Limit12].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  704.        [Limit12].[Id1] AS [Id],
  705.        [Limit12].[Id2] AS [Id1],
  706.        [Limit12].[Id3] AS [Id2],
  707.        [Limit12].[MarketId] AS [MarketId],
  708.        [Limit12].[CompanyId] AS [CompanyId],
  709.        [Limit12].[MarketId1] AS [MarketId1],
  710.        [Limit12].[CompanyId1] AS [CompanyId1],
  711.        [Limit12].[Id] AS [Id3],
  712.        [Limit12].[Id] AS [Id4],
  713.        [Limit12].[Name] AS [Name],
  714.        [Limit12].[DateRegistered] AS [DateRegistered],
  715.        [Limit12].[City] AS [City],
  716.        [Limit12].[StateId] AS [StateId],
  717.        [Limit12].[Phone] AS [Phone],
  718.        [Limit12].[C3] AS [C3],
  719.        [Limit12].[LastLoginTime] AS [LastLoginTime],
  720.        [Limit12].[C1] AS [C4],
  721.        [Limit12].[IsTesting] AS [IsTesting],
  722.        [Limit12].[C2] AS [C5],
  723.        CAST(NULL AS int) AS [C6],
  724.        CAST(NULL AS int) AS [C7],
  725.        CAST(NULL AS varchar(1)) AS [C8],
  726.        CAST(NULL AS varchar(1)) AS [C9],
  727.        CAST(NULL AS varchar(1)) AS [C10],
  728.        CAST(NULL AS varchar(1)) AS [C11],
  729.        [Extent39].[AimAgencyId] AS [AimAgencyId]
  730.        FROM   (SELECT [Project37].[Id] AS [Id], [Project37].[Name] AS [Name], [Project37].[DateRegistered] AS [DateRegistered], [Project37].[IsTesting] AS [IsTesting], [Project37].[SecurityLevel] AS [SecurityLevel], [Project37].[IsDriverEnabled] AS [IsDriverEnabled], [Project37].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns], [Project37].[InterlocationSharingLevel] AS [InterlocationSharingLevel], [Project37].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification], [Project37].[SendEmailsFlag] AS [SendEmailsFlag], [Project37].[AllowViewAcordXml] AS [AllowViewAcordXml], [Project37].[IsAppulateTeam] AS [IsAppulateTeam], [Project37].[UplinkOcrEnabled] AS [UplinkOcrEnabled], [Project37].[LoginLockingEnabled] AS [LoginLockingEnabled], [Project37].[UplinkSettings] AS [UplinkSettings], [Project37].[ShortName] AS [ShortName], [Project37].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType], [Project37].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets], [Project37].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed], [Project37].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations], [Project37].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations], [Project37].[IsVisibleForMarkets] AS [IsVisibleForMarkets], [Project37].[Id1] AS [Id1], [Project37].[City] AS [City], [Project37].[StateId] AS [StateId], [Project37].[Phone] AS [Phone], [Project37].[Id2] AS [Id2], [Project37].[LastLoginTime] AS [LastLoginTime], [Project37].[Id3] AS [Id3], [Project37].[MarketId] AS [MarketId], [Project37].[CompanyId] AS [CompanyId], [Project37].[MarketId1] AS [MarketId1], [Project37].[CompanyId1] AS [CompanyId1], [Project37].[C1] AS [C1], [Project37].[C2] AS [C2], [Project37].[C3] AS [C3]
  731.            FROM ( SELECT
  732.                @p__linq__2 AS [p__linq__2],
  733.                @p__linq__5 AS [p__linq__5],
  734.                @p__linq__6 AS [p__linq__6],
  735.                [Project36].[Id] AS [Id],
  736.                [Project36].[Name] AS [Name],
  737.                [Project36].[DateRegistered] AS [DateRegistered],
  738.                [Project36].[IsTesting] AS [IsTesting],
  739.                [Project36].[SecurityLevel] AS [SecurityLevel],
  740.                [Project36].[IsDriverEnabled] AS [IsDriverEnabled],
  741.                [Project36].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  742.                [Project36].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  743.                [Project36].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  744.                [Project36].[SendEmailsFlag] AS [SendEmailsFlag],
  745.                [Project36].[AllowViewAcordXml] AS [AllowViewAcordXml],
  746.                [Project36].[IsAppulateTeam] AS [IsAppulateTeam],
  747.                [Project36].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  748.                [Project36].[LoginLockingEnabled] AS [LoginLockingEnabled],
  749.                [Project36].[UplinkSettings] AS [UplinkSettings],
  750.                [Project36].[ShortName] AS [ShortName],
  751.                [Project36].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  752.                [Project36].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  753.                [Project36].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  754.                [Project36].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  755.                [Project36].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  756.                [Project36].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  757.                [Project36].[Id1] AS [Id1],
  758.                [Project36].[City] AS [City],
  759.                [Project36].[StateId] AS [StateId],
  760.                [Project36].[Phone] AS [Phone],
  761.                [Project36].[Id2] AS [Id2],
  762.                [Project36].[LastLoginTime] AS [LastLoginTime],
  763.                [Project36].[Id3] AS [Id3],
  764.                [Project36].[MarketId] AS [MarketId],
  765.                [Project36].[CompanyId] AS [CompanyId],
  766.                [Project36].[MarketId1] AS [MarketId1],
  767.                [Project36].[CompanyId1] AS [CompanyId1],
  768.                [Project36].[C1] AS [C1],
  769.                [Project36].[C2] AS [C2],
  770.                [Project36].[C3] AS [C3]
  771.                FROM ( SELECT
  772.                    @p__linq__2 AS [p__linq__2],
  773.                    @p__linq__5 AS [p__linq__5],
  774.                    @p__linq__6 AS [p__linq__6],
  775.                    [Project34].[Id] AS [Id],
  776.                    [Project34].[Name] AS [Name],
  777.                    [Project34].[DateRegistered] AS [DateRegistered],
  778.                    [Project34].[IsTesting] AS [IsTesting],
  779.                    [Project34].[SecurityLevel] AS [SecurityLevel],
  780.                    [Project34].[IsDriverEnabled] AS [IsDriverEnabled],
  781.                    [Project34].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  782.                    [Project34].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  783.                    [Project34].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  784.                    [Project34].[SendEmailsFlag] AS [SendEmailsFlag],
  785.                    [Project34].[AllowViewAcordXml] AS [AllowViewAcordXml],
  786.                    [Project34].[IsAppulateTeam] AS [IsAppulateTeam],
  787.                    [Project34].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  788.                    [Project34].[LoginLockingEnabled] AS [LoginLockingEnabled],
  789.                    [Project34].[UplinkSettings] AS [UplinkSettings],
  790.                    [Project34].[ShortName] AS [ShortName],
  791.                    [Project34].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  792.                    [Project34].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  793.                    [Project34].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  794.                    [Project34].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  795.                    [Project34].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  796.                    [Project34].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  797.                    [Project34].[Id1] AS [Id1],
  798.                    [Project34].[City] AS [City],
  799.                    [Project34].[StateId] AS [StateId],
  800.                    [Project34].[Phone] AS [Phone],
  801.                    [Project34].[Id2] AS [Id2],
  802.                    [Project34].[LastLoginTime] AS [LastLoginTime],
  803.                    [Project34].[Id3] AS [Id3],
  804.                    [Project34].[MarketId] AS [MarketId],
  805.                    [Project34].[CompanyId] AS [CompanyId],
  806.                    [Project34].[MarketId1] AS [MarketId1],
  807.                    [Project34].[CompanyId1] AS [CompanyId1],
  808.                    CASE WHEN ( EXISTS (SELECT
  809.                        1 AS [C1]
  810.                        FROM [dbo].[CompanyToMarket] AS [Extent38]
  811.                        WHERE ([Extent38].[MarketId] = @p__linq__4) AND ([Extent38].[CompanyId] = [Project34].[Id]) AND (0 = [Extent38].[Disabled]) AND (1 = [Extent38].[MarketAccessType])
  812.                    )) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C1],
  813.                    CASE WHEN ([Project34].[AgencyAppointmentStatus] IS NULL) THEN 0 ELSE [Project34].[AgencyAppointmentStatus1] END AS [C2],
  814.                    [Project34].[C1] AS [C3]
  815.                    FROM ( SELECT
  816.                        [Project33].[Id] AS [Id],
  817.                        [Project33].[Name] AS [Name],
  818.                        [Project33].[DateRegistered] AS [DateRegistered],
  819.                        [Project33].[IsTesting] AS [IsTesting],
  820.                        [Project33].[SecurityLevel] AS [SecurityLevel],
  821.                        [Project33].[IsDriverEnabled] AS [IsDriverEnabled],
  822.                        [Project33].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  823.                        [Project33].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  824.                        [Project33].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  825.                        [Project33].[SendEmailsFlag] AS [SendEmailsFlag],
  826.                        [Project33].[AllowViewAcordXml] AS [AllowViewAcordXml],
  827.                        [Project33].[IsAppulateTeam] AS [IsAppulateTeam],
  828.                        [Project33].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  829.                        [Project33].[LoginLockingEnabled] AS [LoginLockingEnabled],
  830.                        [Project33].[UplinkSettings] AS [UplinkSettings],
  831.                        [Project33].[ShortName] AS [ShortName],
  832.                        [Project33].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  833.                        [Project33].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  834.                        [Project33].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  835.                        [Project33].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  836.                        [Project33].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  837.                        [Project33].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  838.                        [Project33].[Id1] AS [Id1],
  839.                        [Project33].[City] AS [City],
  840.                        [Project33].[StateId] AS [StateId],
  841.                        [Project33].[Phone] AS [Phone],
  842.                        [Project33].[Id2] AS [Id2],
  843.                        [Project33].[LastLoginTime] AS [LastLoginTime],
  844.                        [Project33].[Id3] AS [Id3],
  845.                        [Project33].[MarketId] AS [MarketId],
  846.                        [Project33].[CompanyId] AS [CompanyId],
  847.                        [Project33].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus],
  848.                        [Project33].[MarketId1] AS [MarketId1],
  849.                        [Project33].[CompanyId1] AS [CompanyId1],
  850.                        [Project33].[AgencyAppointmentStatus1] AS [AgencyAppointmentStatus1],
  851.                        (SELECT
  852.                            COUNT(1) AS [A1]
  853.                            FROM   (SELECT [Extent35].[MarketId] AS [MarketId], [Extent36].[LocationId] AS [LocationId]
  854.                                FROM  [dbo].[SubmissionPackage] AS [Extent35]
  855.                                INNER JOIN [dbo].[User] AS [Extent36] ON [Extent35].[CreatorId] = [Extent36].[Id]
  856.                                WHERE (0 <> [Extent35].[Status]) AND (3 <> [Extent35].[Status]) ) AS [Filter24]
  857.                            INNER JOIN [dbo].[Location] AS [Extent37] ON [Filter24].[LocationId] = [Extent37].[Id]
  858.                            WHERE ([Filter24].[MarketId] = @p__linq__3) AND ([Extent37].[CompanyId] = [Project33].[Id])) AS [C1]
  859.                        FROM ( SELECT
  860.                            [Project31].[Id] AS [Id],
  861.                            [Project31].[Name] AS [Name],
  862.                            [Project31].[DateRegistered] AS [DateRegistered],
  863.                            [Project31].[IsTesting] AS [IsTesting],
  864.                            [Project31].[SecurityLevel] AS [SecurityLevel],
  865.                            [Project31].[IsDriverEnabled] AS [IsDriverEnabled],
  866.                            [Project31].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  867.                            [Project31].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  868.                            [Project31].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  869.                            [Project31].[SendEmailsFlag] AS [SendEmailsFlag],
  870.                            [Project31].[AllowViewAcordXml] AS [AllowViewAcordXml],
  871.                            [Project31].[IsAppulateTeam] AS [IsAppulateTeam],
  872.                            [Project31].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  873.                            [Project31].[LoginLockingEnabled] AS [LoginLockingEnabled],
  874.                            [Project31].[UplinkSettings] AS [UplinkSettings],
  875.                            [Project31].[ShortName] AS [ShortName],
  876.                            [Project31].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  877.                            [Project31].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  878.                            [Project31].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  879.                            [Project31].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  880.                            [Project31].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  881.                            [Project31].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  882.                            [Project31].[Id1] AS [Id1],
  883.                            [Project31].[City] AS [City],
  884.                            [Project31].[StateId] AS [StateId],
  885.                            [Project31].[Phone] AS [Phone],
  886.                            [Project31].[Id2] AS [Id2],
  887.                            [Project31].[LastLoginTime] AS [LastLoginTime],
  888.                            [Project31].[Id3] AS [Id3],
  889.                            [Project31].[MarketId] AS [MarketId],
  890.                            [Project31].[CompanyId] AS [CompanyId],
  891.                            [Project31].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus],
  892.                            [Limit11].[MarketId] AS [MarketId1],
  893.                            [Limit11].[CompanyId] AS [CompanyId1],
  894.                            [Limit11].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus1]
  895.                            FROM   (SELECT
  896.                                [Project29].[Id] AS [Id],
  897.                                [Project29].[Name] AS [Name],
  898.                                [Project29].[DateRegistered] AS [DateRegistered],
  899.                                [Project29].[IsTesting] AS [IsTesting],
  900.                                [Project29].[SecurityLevel] AS [SecurityLevel],
  901.                                [Project29].[IsDriverEnabled] AS [IsDriverEnabled],
  902.                                [Project29].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  903.                                [Project29].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  904.                                [Project29].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  905.                                [Project29].[SendEmailsFlag] AS [SendEmailsFlag],
  906.                                [Project29].[AllowViewAcordXml] AS [AllowViewAcordXml],
  907.                                [Project29].[IsAppulateTeam] AS [IsAppulateTeam],
  908.                                [Project29].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  909.                                [Project29].[LoginLockingEnabled] AS [LoginLockingEnabled],
  910.                                [Project29].[UplinkSettings] AS [UplinkSettings],
  911.                                [Project29].[ShortName] AS [ShortName],
  912.                                [Project29].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  913.                                [Project29].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  914.                                [Project29].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  915.                                [Project29].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  916.                                [Project29].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  917.                                [Project29].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  918.                                [Project29].[Id1] AS [Id1],
  919.                                [Project29].[City] AS [City],
  920.                                [Project29].[StateId] AS [StateId],
  921.                                [Project29].[Phone] AS [Phone],
  922.                                [Project29].[Id2] AS [Id2],
  923.                                [Project29].[LastLoginTime] AS [LastLoginTime],
  924.                                [Project29].[Id3] AS [Id3],
  925.                                [Limit10].[MarketId] AS [MarketId],
  926.                                [Limit10].[CompanyId] AS [CompanyId],
  927.                                [Limit10].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  928.                                FROM   (SELECT
  929.                                    [Filter20].[Id8] AS [Id],
  930.                                    [Filter20].[Name3] AS [Name],
  931.                                    [Filter20].[DateRegistered] AS [DateRegistered],
  932.                                    [Filter20].[IsTesting] AS [IsTesting],
  933.                                    [Filter20].[SecurityLevel] AS [SecurityLevel],
  934.                                    [Filter20].[IsDriverEnabled] AS [IsDriverEnabled],
  935.                                    [Filter20].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns],
  936.                                    [Filter20].[InterlocationSharingLevel] AS [InterlocationSharingLevel],
  937.                                    [Filter20].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification],
  938.                                    [Filter20].[SendEmailsFlag] AS [SendEmailsFlag],
  939.                                    [Filter20].[AllowViewAcordXml] AS [AllowViewAcordXml],
  940.                                    [Filter20].[IsAppulateTeam] AS [IsAppulateTeam],
  941.                                    [Filter20].[UplinkOcrEnabled] AS [UplinkOcrEnabled],
  942.                                    [Filter20].[LoginLockingEnabled] AS [LoginLockingEnabled],
  943.                                    [Filter20].[UplinkSettings] AS [UplinkSettings],
  944.                                    [Filter20].[ShortName] AS [ShortName],
  945.                                    [Filter20].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType],
  946.                                    [Filter20].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets],
  947.                                    [Filter20].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed],
  948.                                    [Filter20].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations],
  949.                                    [Filter20].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations],
  950.                                    [Filter20].[IsVisibleForMarkets] AS [IsVisibleForMarkets],
  951.                                    [Filter20].[Id9] AS [Id1],
  952.                                    [Filter20].[City] AS [City],
  953.                                    [Filter20].[StateId] AS [StateId],
  954.                                    [Filter20].[Phone] AS [Phone],
  955.                                    [Limit9].[Id] AS [Id2],
  956.                                    [Limit9].[LastLoginTime] AS [LastLoginTime],
  957.                                    [Limit9].[Id1] AS [Id3]
  958.                                    FROM   (SELECT [Extent28].[Id] AS [Id8], [Extent28].[Name] AS [Name3], [Extent28].[DateRegistered] AS [DateRegistered], [Extent28].[IsTesting] AS [IsTesting], [Extent28].[SecurityLevel] AS [SecurityLevel], [Extent28].[IsDriverEnabled] AS [IsDriverEnabled], [Extent28].[MaximumTrialLossRuns] AS [MaximumTrialLossRuns], [Extent28].[InterlocationSharingLevel] AS [InterlocationSharingLevel], [Extent28].[DaysPriorOrderingLossRunnNotification] AS [DaysPriorOrderingLossRunnNotification], [Extent28].[SendEmailsFlag] AS [SendEmailsFlag], [Extent28].[AllowViewAcordXml] AS [AllowViewAcordXml], [Extent28].[IsAppulateTeam] AS [IsAppulateTeam], [Extent28].[UplinkOcrEnabled] AS [UplinkOcrEnabled], [Extent28].[LoginLockingEnabled] AS [LoginLockingEnabled], [Extent28].[UplinkSettings] AS [UplinkSettings], [Extent28].[ShortName] AS [ShortName], [Extent28].[InsuredDeletionRestrictionType] AS [InsuredDeletionRestrictionType], [Extent28].[AllowNonAdminUsersToSelectDeselectMarkets] AS [AllowNonAdminUsersToSelectDeselectMarkets], [Extent28].[IsDecliningSubmissionAllowed] AS [IsDecliningSubmissionAllowed], [Extent28].[EmsRestrictedDesignations] AS [EmsRestrictedDesignations], [Extent28].[EmsAllowUserControlDesignations] AS [EmsAllowUserControlDesignations], [Extent28].[IsVisibleForMarkets] AS [IsVisibleForMarkets], [Extent29].[Id] AS [Id9], [Extent29].[City] AS [City], [Extent29].[StateId] AS [StateId], [Extent29].[Phone] AS [Phone]
  959.                                        FROM  [dbo].[CompanyFlatView] AS [Extent28]
  960.                                        INNER JOIN [dbo].[Location] AS [Extent29] ON [Extent28].[Id] = [Extent29].[CompanyId]
  961.                                        WHERE ([Extent28].[Id] <> @p__linq__0) AND ([Extent28].[DisableDate] IS NULL) AND ([Extent29].[IsPrimaryAddress] = 1) AND ( EXISTS (SELECT
  962.                                            1 AS [C1]
  963.                                            FROM [dbo].[CompanyToMarket] AS [Extent30]
  964.                                            WHERE (1 = [Extent30].[MarketAccessType]) AND ([Extent30].[MarketId] = @p__linq__1) AND ([Extent30].[CompanyId] = [Extent28].[Id])
  965.                                        )) ) AS [Filter20]
  966.                                    OUTER APPLY  (SELECT TOP (1) [Project28].[Id] AS [Id], [Project28].[LastLoginTime] AS [LastLoginTime], [Project28].[Id1] AS [Id1]
  967.                                        FROM ( SELECT
  968.                                            [Extent31].[Id] AS [Id],
  969.                                            [Extent31].[LastLoginTime] AS [LastLoginTime],
  970.                                            [Extent32].[Id] AS [Id1]
  971.                                            FROM  [dbo].[User] AS [Extent31]
  972.                                            INNER JOIN [dbo].[Location] AS [Extent32] ON [Extent31].[LocationId] = [Extent32].[Id]
  973.                                            WHERE [Extent32].[CompanyId] = [Filter20].[Id8]
  974.                                        )  AS [Project28]
  975.                                        ORDER BY [Project28].[LastLoginTime] DESC ) AS [Limit9] ) AS [Project29]
  976.                                OUTER APPLY  (SELECT TOP (1)
  977.                                    [Extent33].[MarketId] AS [MarketId],
  978.                                    [Extent33].[CompanyId] AS [CompanyId],
  979.                                    [Extent33].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  980.                                    FROM [dbo].[CompanyToMarket] AS [Extent33]
  981.                                    WHERE ([Extent33].[MarketId] = @p__linq__7) AND ([Extent33].[CompanyId] = [Project29].[Id]) ) AS [Limit10] ) AS [Project31]
  982.                            OUTER APPLY  (SELECT TOP (1)
  983.                                [Extent34].[MarketId] AS [MarketId],
  984.                                [Extent34].[CompanyId] AS [CompanyId],
  985.                                [Extent34].[AgencyAppointmentStatus] AS [AgencyAppointmentStatus]
  986.                                FROM [dbo].[CompanyToMarket] AS [Extent34]
  987.                                WHERE ([Extent34].[MarketId] = @p__linq__7) AND ([Extent34].[CompanyId] = [Project31].[Id]) ) AS [Limit11]
  988.                        )  AS [Project33]
  989.                    )  AS [Project34]
  990.                )  AS [Project36]
  991.            )  AS [Project37]
  992.            ORDER BY [Project37].[C3] ASC
  993.            OFFSET 20 ROWS FETCH NEXT 20 ROWS ONLY  ) AS [Limit12]
  994.        INNER JOIN [dbo].[AimRetailAgency] AS [Extent39] ON ([Extent39].[MarketId] = @p__linq__6) AND ( EXISTS (SELECT
  995.            1 AS [C1]
  996.            FROM [dbo].[Location] AS [Extent40]
  997.            WHERE ([Extent40].[Id] = [Extent39].[LocationId]) AND ([Extent40].[CompanyId] = [Limit12].[Id]) AND (0 = [Extent40].[IsDeleted])
  998.        ))) AS [UnionAll2]
  999.    ORDER BY [UnionAll2].[C2] ASC, [UnionAll2].[SecurityLevel] ASC, [UnionAll2].[IsDriverEnabled] ASC, [UnionAll2].[MaximumTrialLossRuns] ASC, [UnionAll2].[InterlocationSharingLevel] ASC, [UnionAll2].[DaysPriorOrderingLossRunnNotification] ASC, [UnionAll2].[SendEmailsFlag] ASC, [UnionAll2].[AllowViewAcordXml] ASC, [UnionAll2].[IsAppulateTeam] ASC, [UnionAll2].[UplinkOcrEnabled] ASC, [UnionAll2].[LoginLockingEnabled] ASC, [UnionAll2].[UplinkSettings] ASC, [UnionAll2].[ShortName] ASC, [UnionAll2].[InsuredDeletionRestrictionType] ASC, [UnionAll2].[AllowNonAdminUsersToSelectDeselectMarkets] ASC, [UnionAll2].[IsDecliningSubmissionAllowed] ASC, [UnionAll2].[EmsRestrictedDesignations] ASC, [UnionAll2].[EmsAllowUserControlDesignations] ASC, [UnionAll2].[IsVisibleForMarkets] ASC, [UnionAll2].[Id] ASC, [UnionAll2].[Id1] ASC, [UnionAll2].[Id2] ASC, [UnionAll2].[MarketId] ASC, [UnionAll2].[CompanyId] ASC, [UnionAll2].[MarketId1] ASC, [UnionAll2].[CompanyId1] ASC, [UnionAll2].[Id3] ASC, [UnionAll2].[Name] ASC, [UnionAll2].[DateRegistered] ASC, [UnionAll2].[IsTesting] ASC, [UnionAll2].[C1] ASC',N'@p__linq__0 int,@p__linq__1 int,@p__linq__7 int,@p__linq__3 int,@p__linq__2 int,@p__linq__4 int,@p__linq__5 int,@p__linq__6 int',@p__linq__0=5420,@p__linq__1=5420,@p__linq__7=5420,@p__linq__3=5420,@p__linq__2=5420,@p__linq__4=5420,@p__linq__5=5420,@p__linq__6=5420
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement