Example #1
0
 /// <summary>
 /// Visits the children of <see cref="System.Linq.Expressions.MemberExpression"/>.
 /// </summary>
 /// <param name="node">The expression to visit.</param>
 /// <returns>The modified expression, if it or any subexpression was modified; otherwise,
 /// returns the original expression.</returns>
 protected override Expression VisitMember(MemberExpression node)
 {
     if (node.Member.DeclaringType == typeof(TEntity) ||
         typeof(TEntity).GetTypeInfo().IsSubclassOf(node.Member.DeclaringType))
     {
         var mappedFieldName = "";
         if (this.useTableAlias)
         {
             mappedFieldName = $"T_{storeMapping.GetTableName<TEntity, TKey>()}.{storeMapping.GetEscapedColumnName<TEntity, TKey>(this.dialectSettings, node.Member.Name)}";
         }
         else
         {
             mappedFieldName = $"{storeMapping.GetEscapedTableName<TEntity, TKey>(dialectSettings)}.{storeMapping.GetEscapedColumnName<TEntity, TKey>(this.dialectSettings, node.Member.Name)}";
         }
         Out(mappedFieldName);
     }
     else
     {
         if (node.Member is FieldInfo)
         {
             ConstantExpression ce           = node.Expression as ConstantExpression;
             FieldInfo          fi           = node.Member as FieldInfo;
             object             fieldValue   = fi.GetValue(ce.Value);
             Expression         constantExpr = Expression.Constant(fieldValue);
             Visit(constantExpr);
         }
         else if (node.Member is PropertyInfo)
         {
             if (node.NodeType == ExpressionType.Constant)
             {
                 ConstantExpression ce           = node.Expression as ConstantExpression;
                 PropertyInfo       pi           = node.Member as PropertyInfo;
                 object             fieldValue   = pi.GetValue(ce.Value);
                 Expression         constantExpr = Expression.Constant(fieldValue);
                 Visit(constantExpr);
             }
             else if (node.NodeType == ExpressionType.MemberAccess)
             {
                 var memberExpression = node.Expression as MemberExpression;
                 VisitMember(memberExpression);
             }
         }
         else
         {
             throw new NotSupportedException();
         }
     }
     return(node);
 }
Example #2
0
        public virtual IEnumerable <TEntity> Select(IDbConnection connection,
                                                    Expression <Func <TEntity, bool> > expression = null,
                                                    Sort <TEntity, TKey> sorting = null,
                                                    IDbTransaction transaction   = null)
        {
            IEnumerable <KeyValuePair <string, object> > potentialParameters;
            var sql = this.ConstructSelectStatement(out potentialParameters, expression, sorting);

            var entities = new List <TEntity>();

            using (var command = connection.CreateCommand())
            {
                command.CommandText = sql;
                if (transaction != null)
                {
                    command.Transaction = transaction;
                }
                if (potentialParameters != null)
                {
                    command.Parameters.Clear();
                    foreach (var kvp in potentialParameters)
                    {
                        var param = command.CreateParameter();
                        param.ParameterName = kvp.Key;
                        param.Value         = kvp.Value;
                        command.Parameters.Add(param);
                    }
                }
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var entity = new TEntity();
                        typeof(TEntity)
                        .GetTypeInfo()
                        .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                        .Where(p => p.PropertyType.IsSimpleType() && p.CanWrite)
                        .ToList()
                        .ForEach(x =>
                        {
                            var columnName = $"{mapping.GetTableName<TEntity, TKey>()}_{mapping.GetColumnName<TEntity, TKey>(x)}";
                            var value      = reader[columnName];
                            x.SetValue(entity, EvaluatePropertyValue(x, value));
                        });
                        entities.Add(entity);
                    }
                    reader.Close();
                }
            }
            return(entities);
        }