Exemple #1
0
        protected void Populate <TForeignEntity, TForeignEntityPrimaryKey>(Expression <Func <TForeignEntity, TPrimaryKey> > foreignKey,
                                                                           Expression <Func <TEntity, IList <TForeignEntity> > > foreignEntityCollection,
                                                                           params TEntity[] entities)
            where TForeignEntity : class, IIdentifier <TForeignEntityPrimaryKey>, new()
        {
            if (!entities.Any())
            {
                return;
            }

            ITableInfoBase foreignTableInfo = _queryBuilder.GetBaseTableInfo <TForeignEntity>();
            string         foreignKeyColum  = GetForeignKeyColumn <TForeignEntity, TForeignEntityPrimaryKey>(foreignKey);

            string sql = string.Format("SELECT * FROM {0} WHERE {1} IN @ForeignKeys", foreignTableInfo.TableName, foreignKeyColum);
            IEnumerable <TPrimaryKey> keys = entities.Select(e => e.GetIdentity());
            IList <TForeignEntity>    foreignEntities;

            using (IDbConnection connection = CreateConnection())
            {
                foreignEntities = connection.Query <TForeignEntity>(sql, new { ForeignKeys = keys }).ToList();
            }

            Action <TEntity, IList <TForeignEntity> > setter        = GetSetter(foreignEntityCollection);
            Func <TForeignEntity, TPrimaryKey>        getForeignKey = foreignKey.Compile();

            foreach (TEntity entity in entities)
            {
                TPrimaryKey key = entity.GetIdentity();
                setter(entity, foreignEntities.Where(se => Equals(getForeignKey(se), key)).ToList());
            }
        }
Exemple #2
0
        public virtual Contact GetContactByName(string name)
        {
            ITableInfoBase tableInfo = GetTableInfo();
            string         sql       = string.Format(@"SELECT * FROM {0} WHERE Name = @Name", tableInfo.TableName);

            using (IDbConnection connection = CreateConnection())
            {
                return(connection.Query <Contact>(sql, new { Name = name }).SingleOrDefault());
            }
        }