The code from Kean's blog that you were referred to by another reply is not really the best way to solve the problem, as it will not work with dynamic blocks, and only gives you blocks inserted into model or paper space (which may or may not be what you intended). Since you didn't mention if you wanted only those BlockReferences in the current space, model & paper space, or all block references, including those inserted into other blocks, I'll assume nothing, since a robust solution will give you the option.
There is a much faster and more direct way to solve the problem, which gives you the option of including every BlockReference regardless of what owner block it's inserted into, AND also gives you all dynamic BlockReferences.
public static class AcDbLinqExtensionMethods
{
/// <summary>
/// Get all references to the given BlockTableRecord, including
/// references to anonymous dynamic BlockTableRecords.
/// </summary>
public static IEnumerable<BlockReference> GetBlockReferences(
this BlockTableRecord btr,
OpenMode mode = OpenMode.ForRead,
bool directOnly = true )
{
if( btr == null )
throw new ArgumentNullException( "btr" );
var tr = btr.Database.TransactionManager.TopTransaction;
if( tr == null )
throw new InvalidOperationException( "No transaction" );
var ids = btr.GetBlockReferenceIds( directOnly, true );
int cnt = ids.Count;
for( int i = 0; i < cnt; i++ )
{
yield return (BlockReference)
tr.GetObject( ids[i], mode, false, false );
}
if( btr.IsDynamicBlock )
{
BlockTableRecord btr2 = null;
var blockIds = btr.GetAnonymousBlockIds();
cnt = blockIds.Count;
for( int i = 0; i < cnt; i++ )
{
btr2 = (BlockTableRecord) tr.GetObject( blockIds[i],
OpenMode.ForRead, false, false );
ids = btr2.GetBlockReferenceIds( directOnly, true );
int cnt2 = ids.Count;
for( int j = 0; j < cnt2; j++ )
{
yield return (BlockReference)
tr.GetObject( ids[j], mode, false, false );
}
}
}
}
}
Using the above, you just open the BlockTableRecord whose references you need, and call GetBlockReferences() on the open BlockTableRecord, and use foreach() to iterate over the blocks. If you only want block references from model space, you can simply check the BlockId property of each BlockReference and compare it to the ObjectId of the model space BlockTableRecord, to filter out BlockReferences that aren't in model space. If you wanted only BlockReferences inserted into paper space, you can do the same as described above, except comparing the BlockId property of each BlockReference to the ObjectId of the paper space BlockTableRecord.