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();
}