Rock.Model.LocationService.GetByDevice C# (CSharp) Method

GetByDevice() public method

Gets the locations associated to a device and optionally any child locaitons
public GetByDevice ( int deviceId, bool includeChildLocations = true ) : IEnumerable
deviceId int The device identifier.
includeChildLocations bool if set to true [include child locations].
return IEnumerable
        public IEnumerable<Location> GetByDevice( int deviceId, bool includeChildLocations = true )
        {
            string childQuery = includeChildLocations ? @"

            UNION ALL

            SELECT [a].*
            FROM [Location] [a]
            INNER JOIN  CTE pcte ON pcte.Id = [a].[ParentLocationId]
            WHERE [a].[ParentLocationId] IS NOT NULL
            " : "";

            return ExecuteQuery( string.Format(
                @"
            WITH CTE AS (
            SELECT L.*
            FROM [DeviceLocation] D
            INNER JOIN [Location] L ON L.[Id] = D.[LocationId]
            WHERE D.[DeviceId] = {0}
            {1}
            )

            SELECT * FROM CTE
            ", deviceId, childQuery ) );
        }

Usage Example

Example #1
0
        /// <summary>
        /// Gets the device group types.
        /// </summary>
        /// <param name="deviceId">The device identifier.</param>
        /// <returns></returns>
        private List<GroupType> GetDeviceGroupTypes( int deviceId )
        {
            var groupTypes = new Dictionary<int, GroupType>();

            var locationService = new LocationService( new RockContext() );

            // Get all locations (and their children) associated with device
            var locationIds = locationService
                .GetByDevice(deviceId, true)
                .Select( l => l.Id)
                .ToList();

            // Requery using EF
            foreach ( var groupType in locationService.Queryable()
                .Where( l => locationIds.Contains( l.Id ) )
                .SelectMany( l => l.GroupLocations )
                .Select( gl => gl.Group.GroupType )
                .ToList() )
            {
                if ( !groupTypes.ContainsKey( groupType.Id ) )
                {
                    groupTypes.Add( groupType.Id, groupType );
                }
            }

            return groupTypes.Select( g => g.Value ).ToList();
        }
All Usage Examples Of Rock.Model.LocationService::GetByDevice