internal static IEnumerable <T> AsEnumerable <T>(this IDataReader reader)
            where T : DataEntity
        {
            var properties = DataEntityExtension.GetPropertiesFor <T>(Command.None)
                             .Where(property => property.CanWrite);
            var dictionary = new Dictionary <int, PropertyInfo>();

            for (var i = 0; i < reader.FieldCount; i++)
            {
                var property = properties.FirstOrDefault(p => p.GetMappedName().ToLower() == reader.GetName(i).ToLower());
                if (property != null)
                {
                    dictionary.Add(i, property);
                }
            }
            var list = new List <T>();

            while (reader.Read())
            {
                var obj = Activator.CreateInstance <T>();
                foreach (var kvp in dictionary)
                {
                    var value = reader.IsDBNull(kvp.Key) ? null : reader.GetValue(kvp.Key);
                    kvp.Value.SetValue(obj, value);
                }
                list.Add(obj);
            }
            return(list);
        }
示例#2
0
        /// <summary>
        /// Merges an object into an instance of <see cref="QueryGroup"/> object.
        /// </summary>
        /// <param name="obj">The object to be merged.</param>
        /// <param name="queryGroup">The <see cref="QueryGroup"/> object to be merged.</param>
        /// <returns>An instance of converted dynamic object.</returns>
        internal static object AsMergedObject(this object obj, QueryGroup queryGroup)
        {
            var expandObject = new ExpandoObject() as IDictionary <string, object>;
            var properties   = DataEntityExtension.GetProperties(obj.GetType());

            foreach (var property in properties)
            {
                expandObject[property.GetMappedName()] = property.PropertyInfo.GetValue(obj);
            }
            if (queryGroup != null)
            {
                foreach (var queryField in queryGroup.Fix().GetAllQueryFields())
                {
                    expandObject[queryField.Parameter.Name] = queryField.Parameter.Value;
                }
            }
            return((ExpandoObject)expandObject);
        }