/// <summary>
        /// Select a model or collection of models to include in the dataset with a foreign key relating to the root collection.
        /// </summary>
        /// <typeparam name="TSource">Type of the root collection.</typeparam>
        /// <typeparam name="TModel">Type of the selected model.</typeparam>
        /// <typeparam name="TForeignKey">Type of the foreign key</typeparam>
        /// <param name="me">Collection of field selectors.</param>
        /// <param name="field">The field to include.</param>
        /// <param name="foreignKey">Function returning the foreign key value.</param>
        /// <param name="foreignKeyName">Optional name for the foreign key column.</param>
        /// <returns>Collection of current the field selectors.</returns>
        public static FieldSelectorCollection <TSource> SelectModel <TSource, TModel, TForeignKey>(
            this FieldSelectorCollection <TSource> me, Func <TSource, TModel> field, Func <TSource, TForeignKey> foreignKey, string foreignKeyName)
            where TSource : class
            where TModel : class
        {
            ValidateModelType(typeof(TModel));
            me?.Add(new FieldSelector <TSource, TModel, TForeignKey>(field, foreignKey, foreignKeyName));

            return(me);
        }
Esempio n. 2
0
        protected virtual IEnumerable <IEnumerable <DataTable> > FillDataTables <T>(
            IEnumerable <T> data, FieldSelectorCollection <T> fieldSelectors, int dumpEvery)
            where T : class
        {
            EventHandler <RowCreatedEventArgs> rowCreated = null;

            var tables = new Dictionary <string, DataTable>();

            foreach (var model in data)
            {
                foreach (var selector in fieldSelectors)
                {
                    var name = GetTableName(selector.FieldType, selector.TableName());
                    if (!tables.TryGetValue(name, out var table))
                    {
                        tables.Add(
                            name,
                            (table = GetDataTableSchema(selector.FieldType, selector.TableName()))
                            );
                    }

                    var value = selector.GetField(model);

                    HandleForeignKeyField(
                        selector as IFieldSelectorWithForeignKey <T>,
                        model,
                        table,
                        ref rowCreated
                        );

                    var tempResults = FillDataTable(
                        selector.FieldType,
                        table,
                        value as IEnumerable ?? new[] { value },
                        dumpEvery
                        );

                    foreach (var result in tempResults)
                    {
                        if (tables.Values.Sum(x => x.Rows.Count) >= dumpEvery)
                        {
                            yield return(tables.Values);
                        }
                    }
                }
            }

            if (rowCreated != null)
            {
                RowCreated -= rowCreated;
            }

            yield return(tables.Values);
        }
        /// <summary>
        /// Select a model or collection of models to include in the dataset with a foreign key relating to a specific root.
        /// </summary>
        /// <typeparam name="TSource">Type of the root collection.</typeparam>
        /// <typeparam name="TModel">Type of the selected model.</typeparam>
        /// <typeparam name="TForeignKeyRoot">Type of the model containing the foreign key field.</typeparam>
        /// <typeparam name="TForeignKey">Type of the foreign key</typeparam>
        /// <param name="me">Collection of field selectors.</param>
        /// <param name="field">The field to include with its specific root.</param>
        /// <param name="foreignKey">Function returning the foreign key value.</param>
        /// <returns>Collection of current the field selectors.</returns>
        public static FieldSelectorCollection <TSource> SelectNestedModel <TSource, TModel, TForeignKeyRoot, TForeignKey>(
            this FieldSelectorCollection <TSource> me,
            Func <TSource, IEnumerable <IForeignKeySelect <TForeignKeyRoot, TModel> > > field,
            Func <TForeignKeyRoot, TForeignKey> foreignKey)
            where TSource : class
            where TModel : class
            where TForeignKeyRoot : class
        {
            me?.Add(new FieldSelector <TSource, TModel, TForeignKeyRoot, TForeignKey>(field, foreignKey));

            return(me);
        }