public static T GetDataGridViewRowCellValue <T>(DataGridViewRow dataGridViewRow, string columnName, bool resultIsValueType) { DataRow row = ((DataRowView)(dataGridViewRow.DataBoundItem)).Row; object result = row[columnName]; if (resultIsValueType) { if (result == null) { return(default(T)); } try { return(EntityReader.ConvertValueTypeTo <T>(result)); } catch (Exception ex) { throw ex; } } else { return((T)result); } }
public object GetEntity(string key, bool throwExceptionOnNotFound, Type entityType) { EnsureConnection(); if (!_database.KeyExists(key)) { if (throwExceptionOnNotFound) { throw new NullReferenceException($"Could not find entity with key {key}."); } return(null); } HashEntry[] hashSet = _database.HashGetAll(key); object result = Activator.CreateInstance(entityType); foreach (var property in entityType.GetProperties()) { HashEntry entry = hashSet.Where(e => e.Name.Equals(property.Name)).FirstOrDefault(); if (entry == null) { throw new NullReferenceException($"Could not find {nameof(HashEntry)} for property {property.Name} for entity {entityType.Name}."); } object value = EntityReader.ConvertValueTypeTo(entry.Value, property.PropertyType); property.SetValue(result, value); } return(result); }
/// <summary> /// Queries for entities in a table corresponding to entity type. The query is performed on the column/field /// specified with the specified field value. /// </summary> /// <typeparam name="E">The entity type i.e. the table from which the entities will be returned.</typeparam> /// <param name="fieldName">The name of the field/column on which the query will be performed.</param> /// <param name="fieldValue">The value of the field which will be used for the query.</param> /// <param name="loadChildren">Whether or not to load the children of the entities as well.</param> /// <returns>Returns a list of entities of the specified type with the specified field/column and field value.</returns> public virtual List <E> GetEntitiesByField <E>(string fieldName, object fieldValue, bool loadChildren) where E : class { SetDeferredLoadingEnabled(loadChildren); Type entityType = typeof(E); PropertyInfo field = entityType.GetProperty(fieldName); object keyValueConverted = EntityReader.ConvertValueTypeTo(fieldValue, field.PropertyType); ParameterExpression e = Expression.Parameter(entityType, "e"); MemberExpression memberExpression = Expression.MakeMemberAccess(e, field); //Name of surrogate key : left hand side of the expression. ConstantExpression constantExpression = Expression.Constant(keyValueConverted, field.PropertyType); //Value of the surrogate key : right hand side of the expression. BinaryExpression binaryExpression = Expression.Equal(memberExpression, constantExpression); Expression <Func <E, bool> > lambdaExpression = Expression.Lambda <Func <E, bool> >(binaryExpression, e); return(DB.GetTable <E>().Where(lambdaExpression).ToList()); }
/// <summary> /// Queries for and returns an entity from the table corresponding to the entity type. The query is performed /// on the surrogate key of the entity. /// </summary> /// <typeparam name="E">The entity type i.e. which table the entity will be queried from.</typeparam> /// <param name="keyValue">The value of the surrogate to search by.</param> /// <param name="loadChildren">Whether or not to load the children entities onto this entity.</param> /// <param name="throwExceptionOnNotFound">Whether or not to throw an exception if the result is null.</param> /// <returns>Returns an entity with the specified type and surrogate key. Returns null if one is not found.</returns> public virtual E GetEntityBySurrogateKey <E>(object keyValue, bool loadChildren, bool throwExceptionOnNotFound) where E : class { SetDeferredLoadingEnabled(loadChildren); Type entityType = typeof(E); PropertyInfo surrogateKey = GetEntitySurrogateKey(entityType); object keyValueConverted = EntityReader.ConvertValueTypeTo(keyValue, surrogateKey.PropertyType); ParameterExpression e = Expression.Parameter(entityType, "e"); MemberExpression memberExpression = Expression.MakeMemberAccess(e, surrogateKey); //Name of surrogate key : left hand side of the expression. ConstantExpression constantExpression = Expression.Constant(keyValueConverted, surrogateKey.PropertyType); //Value of the surrogate key : right hand side of the expression. BinaryExpression binaryExpression = Expression.Equal(memberExpression, constantExpression); Expression <Func <E, bool> > lambdaExpression = Expression.Lambda <Func <E, bool> >(binaryExpression, e); E result = DB.GetTable <E>().SingleOrDefault(lambdaExpression); if (result == null && throwExceptionOnNotFound) { throw new NullReferenceException($"{nameof(GetEntityBySurrogateKey)} could not find entity {typeof(E).Name} with key value of '{keyValue}'."); } return(result); }
public static T GetDataGridViewRowCellValue <T>(DataGridView grid, int rowIndex, string columnName, bool resultIsValueType) { if ((rowIndex >= grid.Rows.Count) || (rowIndex < 0)) { return(default(T)); } DataRow row = ((DataRowView)(grid.Rows[rowIndex].DataBoundItem)).Row; object result = row[columnName]; if (resultIsValueType) { if (result == null) { return(default(T)); } return(EntityReader.ConvertValueTypeTo <T>(result)); } else { return((T)result); } }