public Task <AsyncResult <IEnumerable <Tuple <string, int, string> > > > GetUserSelectedAdditionalDataListAsync(Guid profileId, string[] keys, bool sortByKey = false, SortDirection sortDirection = SortDirection.Ascending,
                                                                                                                        uint?offset = null, uint?limit = null)
        {
            ISQLDatabase database    = ServiceRegistration.Get <ISQLDatabase>();
            ITransaction transaction = database.BeginTransaction();

            try
            {
                int dataNoIndex;
                int dataIndex;
                int keyIndex;
                List <Tuple <string, int, string> > list = new List <Tuple <string, int, string> >();
                using (IDbCommand command = UserProfileDataManagement_SubSchema.SelectUserAdditionalDataListCommand(transaction, profileId,
                                                                                                                    keys, sortByKey, sortDirection, out keyIndex, out dataNoIndex, out dataIndex))
                {
                    using (IDataReader reader = command.ExecuteReader())
                    {
                        var records = reader.AsEnumerable();
                        if (offset.HasValue)
                        {
                            records = records.Skip((int)offset.Value);
                        }
                        if (limit.HasValue)
                        {
                            records = records.Take((int)limit.Value);
                        }
                        foreach (var record in records)
                        {
                            list.Add(new Tuple <string, int, string>(database.ReadDBValue <string>(record, keyIndex), database.ReadDBValue <int>(record, dataNoIndex),
                                                                     database.ReadDBValue <string>(record, dataIndex)));
                        }
                    }
                }
                IEnumerable <Tuple <string, int, string> > data = null;
                if (list.Count > 0)
                {
                    data = list;
                }
                return(Task.FromResult(new AsyncResult <IEnumerable <Tuple <string, int, string> > >(data != null, data)));
            }
            finally
            {
                transaction.Dispose();
            }
        }
        //TODO: DbCommand Async call?
        protected Task <ICollection <UserProfile> > GetProfiles(Guid?profileId, string name, bool loadData = true)
        {
            ISQLDatabase database    = ServiceRegistration.Get <ISQLDatabase>();
            ITransaction transaction = database.BeginTransaction();

            try
            {
                int profileIdIndex;
                int nameIndex;
                int idIndex;
                int dataIndex;
                int lastLoginIndex;
                int imageIndex;
                ICollection <UserProfile> result = new List <UserProfile>();
                using (IDbCommand command = UserProfileDataManagement_SubSchema.SelectUserProfilesCommand(transaction, profileId, name,
                                                                                                          out profileIdIndex, out nameIndex, out idIndex, out dataIndex, out lastLoginIndex, out imageIndex))
                {
                    using (IDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            result.Add(new UserProfile(
                                           database.ReadDBValue <Guid>(reader, profileIdIndex),
                                           database.ReadDBValue <string>(reader, nameIndex),
                                           (UserProfileType)database.ReadDBValue <int>(reader, idIndex),
                                           database.ReadDBValue <string>(reader, dataIndex),
                                           database.ReadDBValue <DateTime?>(reader, lastLoginIndex),
                                           database.ReadDBValue <byte[]>(reader, imageIndex))
                                       );
                        }
                    }
                }

                if (loadData)
                {
                    foreach (var user in result)
                    {
                        using (IDbCommand command = UserProfileDataManagement_SubSchema.SelectUserAdditionalDataListCommand(transaction, user.ProfileId, null, false, SortDirection.Ascending,
                                                                                                                            out nameIndex, out profileIdIndex, out dataIndex))
                        {
                            using (IDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    string key = database.ReadDBValue <string>(reader, nameIndex);
                                    if (!user.AdditionalData.ContainsKey(key))
                                    {
                                        user.AdditionalData.Add(key, new Dictionary <int, string>());
                                    }
                                    user.AdditionalData[key].Add(database.ReadDBValue <int>(reader, profileIdIndex), database.ReadDBValue <string>(reader, dataIndex));
                                }
                            }
                        }
                    }
                }

                return(Task.FromResult(result));
            }
            finally
            {
                transaction.Dispose();
            }
        }