/// <summary> /// Retrieves all the entities of the specified type with a reference to any of the provided entities. /// </summary> /// <param name="propertyName">The name of the property containing the reference.</param> /// <param name="referencedEntities">An array of entities to check the reference to.</param> /// <returns>An array of the references retrieved.</returns> public override T[] GetEntitiesWithReference <T>(string propertyName, IEntity[] referencedEntities) { List <T> entities = null; using (LogGroup logGroup = LogGroup.StartDebug("Querying the data store based on the provided parameters.")) { LogWriter.Debug("Property name: " + propertyName); LogWriter.Debug("Referenced entities #: " + referencedEntities.Length); if (referencedEntities.Length > 0) { Type referencedEntityType = referencedEntities[0].GetType(); if (referencedEntityType != null) { LogWriter.Debug("Referenced entity type: " + referencedEntityType.ToString()); } else { LogWriter.Debug("Referenced entity type: [null]"); } Type type = typeof(T); EntityReferenceCollection references = new EntityReferenceCollection(); foreach (IEntity referencedEntity in referencedEntities) { // Load the references all in one go, to avoid individual loads references.AddRange(Provider.Referencer.GetReferences(referencedEntity.GetType(), referencedEntity.ID, typeof(T), false)); } Db4oDataStore store = (Db4oDataStore)GetDataStore(type); IObjectContainer container = store.ObjectContainer; entities = new List <T>( container .Query <T>( delegate(T e) { bool matches = true; using (LogGroup logGroup2 = LogGroup.Start("Querying entity.", NLog.LogLevel.Debug)) { LogWriter.Debug("Checking type " + e.GetType().ToString()); LogWriter.Debug("Entity ID: " + e.ID); bool foundReference = references.Includes(e.ID, propertyName); // If referenced entities were provided then entities match if a reference exists if (referencedEntities != null && referencedEntities.Length > 0) { matches = foundReference; } // Otherwise the calling code is trying to get entities where NO reference exists, therefore it matches when no reference is found else { matches = !foundReference; } LogWriter.Debug("Matches: " + matches); } return(matches); })); if (entities != null) { LogWriter.Debug("entities != null"); } else { LogWriter.Debug("entities == null"); } LogWriter.Debug("Total objects: " + entities.Count); } } return(Release(entities.ToArray())); }