Пример #1
0
        /// <summary>
        /// Return all the records. T should be interface with getter properties that match types and names of the database.
        /// Optionally instead of T being and interface you can pass in an anonymous object with properties that match that
        /// database and then you'll get an IEnumerable of that anonymous type with the data filled in.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader">The reader.</param>
        /// <param name="prototype">The prototype. Anonymous class instance</param>
        /// <returns></returns>
        public static IEnumerable <T> AllRecords <T>(this DBFReader reader, T prototype = null) where T : class
        {
            var tType = typeof(T);

            var tProperties = tType.GetProperties()
                              .Where(
                it =>
                Array.FindIndex(reader.Fields,
                                f => f.Name.Equals(it.Name, StringComparison.InvariantCultureIgnoreCase)) >= 0)
                              .ToList();
            var tProps = tProperties
                         .Select(
                it =>
                Array.FindIndex(reader.Fields,
                                jt => jt.Name.Equals(it.Name, StringComparison.InvariantCultureIgnoreCase)))
                         .Where(it => it >= 0)
                         .ToArray();

            var tOrderedProps = tProps.OrderBy(it => it).ToArray();
            var tReturn       = new List <T>();


            if (tType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Any())
            {
                var tAnon = reader.NextRecord(tProps, tOrderedProps);
                while (tAnon != null)
                {
                    tReturn.Add((T)Activator.CreateInstance(tType, tAnon));
                    tAnon = reader.NextRecord(tProps, tOrderedProps);
                }


                return(tReturn);
            }

            var t = reader.NextRecord(tProps, tOrderedProps);

            while (t != null)
            {
                var interceptor = new Enumerable.DBFInterceptor(t, tProperties.Select(it => it.Name).ToArray());

                tReturn.Add(interceptor.ActLike <T>(typeof(Enumerable.IDBFInterceptor)));
                t = reader.NextRecord(tProps, tOrderedProps);
            }


            return(tReturn);
        }
Пример #2
0
        /// <summary>
        /// Returns a list of dynamic objects whose properties and types match up with that database name.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="whereColumn">The where column name.</param>
        /// <param name="whereColumnEquals">What the were column should equal.</param>
        /// <returns></returns>
        public static IEnumerable <dynamic> DynamicAllRecords(this DBFReader reader, string whereColumn = null,
                                                              dynamic whereColumnEquals = null)
        {
            var props = reader.GetSelectFields().Select(it => it.Name).ToArray();

            int?whereColumnIndex = null;

            if (!String.IsNullOrEmpty(whereColumn))
            {
                whereColumnIndex = Array.FindIndex(props,
                                                   it => it.Equals(whereColumn, StringComparison.InvariantCultureIgnoreCase));
            }


            var tReturn = new List <object>();
            var t       = reader.NextRecord();

            while (t != null)
            {
                if (whereColumnIndex is int i)
                {
                    dynamic tO = t[i];
                    if (!tO.Equals(whereColumnEquals))
                    {
                        t = reader.NextRecord();
                        continue;
                    }
                }


                var interceptor = new Enumerable.DBFInterceptor(t, props);


                tReturn.Add(interceptor);
                t = reader.NextRecord();
            }


            return(tReturn);
        }