private XElement BuildRow(Entity entity, XElement subEntityTable, XElement subItemTable, XElement subDictionaryTable)
        {
            var entityPropertyValueEntities = entity.GetPropertyValues().Where(cur => cur.Value is Entity);
            var entityPropertyValuesWithoutEntitiesOrLists = entity.GetPropertyValues().Where(cur => !(cur.Value is Entity || cur.Value is IEnumerable<object> || cur.Value is IDictionary));
            var subEntityListPropertyValues = entity.GetPropertyValues().Where(cur => cur.Value is IEnumerable<Entity>);
            var subItemListPropertyValues = entity.GetPropertyValues().Where(cur => cur.Value is IEnumerable<object> && !(cur.Value is IEnumerable<Entity>));
            var subDictionaryPropertyValues = entity.GetPropertyValues().Where(cur => cur.Value is IDictionary);

            subDictionaryTable.Add(subDictionaryPropertyValues.SelectMany(dictPropertyValue =>
            {
                var result = new List<XElement>();

                var dict = (IDictionary)dictPropertyValue.Value;
                var keys = dict.Keys.Cast<object>();
                var values = dict.Values.Cast<object>();
                for (int i = 0; i < dict.Count; i++)
                {
                    var key = keys.ElementAt(i);
                    var value = values.ElementAt(i);

                    XAttribute keyAttribute = null;
                    XAttribute valueAttribute = null;

                    if (key is Entity)
                        keyAttribute = new XAttribute("KeyEntityId", ((Entity)key).EntityId.Id);
                    else
                        keyAttribute = new XAttribute("Key", key ?? string.Empty);

                    if (value is Entity)
                        valueAttribute = new XAttribute("ValueEntityId", ((Entity)value).EntityId.Id);
                    else
                        valueAttribute = new XAttribute("Value", value ?? string.Empty);

                    result.Add(new XElement("Entry",
                        new XAttribute("OwnerId", entity.EntityId.Id),
                        new XAttribute("OwnerCollection", dictPropertyValue.Key),
                        keyAttribute,
                        valueAttribute));
                }

                return result;
            }));

            subEntityTable.Add(subEntityListPropertyValues.SelectMany(curSubEntityListPropertyValue =>
                from subEntity in (IEnumerable<Entity>)curSubEntityListPropertyValue.Value
                select new XElement("Entry",
                    new XAttribute("OwnerId", entity.EntityId.Id),
                    new XAttribute("OwnerCollection", curSubEntityListPropertyValue.Key),
                    new XAttribute("EntityId", subEntity.EntityId.Id))));

            subItemTable.Add(subItemListPropertyValues.SelectMany(curSubItemListPropertyValue =>
                from subItem in (IEnumerable<object>)curSubItemListPropertyValue.Value
                select new XElement("Entry",
                    new XAttribute("OwnerId", entity.EntityId.Id),
                    new XAttribute("OwnerCollection", curSubItemListPropertyValue.Key),
                    new XAttribute("Value", subItem.ToString()))));

            return new XElement("Entry",
                new XAttribute("EntityId", entity.EntityId.Id),
                from propertyValue in entityPropertyValueEntities
                select new XAttribute(propertyValue.Key, ((Entity)propertyValue.Value).EntityId.Id),
                from propertyValue in entityPropertyValuesWithoutEntitiesOrLists
                select new XAttribute(propertyValue.Key, propertyValue.Value ?? ""));
        }
        private IEnumerable<Entity> GetSubEntities(Entity entity, IEnumerable<Entity> except)
        {
            List<Entity> thisSubEntities = new List<Entity>();
            Dictionary<string, object> entityPropertyValues = entity.GetPropertyValues();
            var entityPropertyValueEntities = (from propertyValue in entityPropertyValues
                                               where propertyValue.Value is Entity
                                               select (Entity)propertyValue.Value).ToList();
            var subEntityListPropertyValues = entityPropertyValues
                .Where(cur => cur.Value is IEnumerable<Entity>)
                .SelectMany(cur => (IEnumerable<Entity>)cur.Value).ToList();
            var subDictionaryPropertyValues = entityPropertyValues
                .Where(cur => cur.Value is IDictionary)
                .SelectMany(cur =>
                    {
                        var dictionary = (IDictionary)cur.Value;
                        return dictionary.Keys.OfType<Entity>().Concat(dictionary.Values.OfType<Entity>());
                    }).ToList();

            thisSubEntities.AddRange(entityPropertyValueEntities);
            thisSubEntities.AddRange(subEntityListPropertyValues);
            thisSubEntities.AddRange(subDictionaryPropertyValues);
            foreach (var subEntity in entityPropertyValueEntities.Concat(subEntityListPropertyValues).Except(except))
            {
                var subEntities = GetSubEntities(subEntity, except.Concat(thisSubEntities));
                if (subEntities.Count() > 0)
                    thisSubEntities.AddRange(subEntities);
            }

            return thisSubEntities;
        }