Rock.Data.RockUdfHelper.ufnCrm_GetFamilyTitle C# (CSharp) Method

ufnCrm_GetFamilyTitle() public static method

calls database TVF function ufnCrm_GetFamilyTitle. Usage: string familyTitle = RockUdfHelper.ufnCrm_GetFamilyTitle( rockContext, personId, groupId, commaPersonIds, true );
public static ufnCrm_GetFamilyTitle ( RockContext rockContext, int PersonId, int GroupId, string GroupPersonIds, bool UseNickName ) : string
rockContext RockContext The rock context.
PersonId int The PersonId. NULL means use GroupId parameter
GroupId int The GroupId of the Family. NULL means use PersonId parameter
GroupPersonIds string If GroupId is specified, set this as a comma-delimited list of PersonIds that you want to limit the family members to. NULL means don't restrict.
UseNickName bool if set to true [use nick name].
return string
        public static string ufnCrm_GetFamilyTitle( RockContext rockContext, int? PersonId, int? GroupId, string GroupPersonIds, bool UseNickName )
        {
            var result = rockContext.Database.SqlQuery(
                typeof( string ),
                "SELECT TOP 1 [PersonNames] FROM dbo.ufnCrm_GetFamilyTitle(@PersonId, @GroupId, @GroupPersonIds, @UseNickName)",
                new SqlParameter( "@PersonId", PersonId.HasValue ? (object)PersonId.Value : DBNull.Value ) { SqlDbType = SqlDbType.Int, IsNullable = true },
                new SqlParameter( "@GroupId", GroupId.HasValue ? (object)GroupId.Value : DBNull.Value ) { SqlDbType = SqlDbType.Int, IsNullable = true },
                new SqlParameter( "@GroupPersonIds", string.IsNullOrWhiteSpace( GroupPersonIds ) ? DBNull.Value : (object)GroupPersonIds ) { SqlDbType = SqlDbType.Text, IsNullable = true },
                new SqlParameter( "@UseNickName", UseNickName ));

            // NOTE: ufnCrm_GetFamilyTitle is a Table Valued Function, but it only returns one ROW
            return result.OfType<string>().FirstOrDefault();
        }