/// <summary>
        /// Filter the sequence of values base on a predicate
        /// and Specifies the related objects to include in the query results
        /// </summary>
        /// <typeparam name="TProperty">entity refernced by foreign key</typeparam>
        /// <param name="predicate">predicate</param>
        /// <param name="includePath">The related object to return in the query results.</param>
        /// <param name="foreignSource">source of foreign type entities</param>
        /// <returns>A new System.Linq.IQueryable<T> with the defined query path.</returns>
        public IQueryable <T> GetAllAndInclude <TProperty>(Expression <Func <T, bool> > predicate, Expression <Func <T, TProperty> > includePath, IEnumerable <TProperty> foreignSource) where TProperty : class
        {
            Action <T, TProperty> setter;

            var foreignKeyPredicate = TestRepositoryHelper.GetForeinKeyPredicate(includePath, out setter).Compile();

            var includeQuery = AsEnumerable()
                               .Where(predicate.Compile())
                               .Select(item =>
            {
                var include = foreignSource.FirstOrDefault(foreign =>
                {
                    return(foreignKeyPredicate(item, foreign));
                });

                if (include != null)
                {
                    setter(item, include);
                }

                return(item);
            });

            return(includeQuery.AsQueryable());
        }
Esempio n. 2
0
        /// <summary>
        /// Specifies the related objects to include in the query results. Differs Entity Framework and Test repositories
        /// </summary>
        /// <typeparam name="T">Entity type</typeparam>
        /// <typeparam name="TProperty">related object type</typeparam>
        /// <param name="source">The source System.Linq.IQueryable<T> on which to call Include.</param>
        /// <param name="includePath">The related object to return in the query results.</param>
        /// <param name="foreignSource">source of foreign type entities</param>
        /// <returns></returns>
        public static IQueryable <T> Include <T, TProperty>(this IQueryable <T> source, Expression <Func <T, TProperty> > includePath, IEnumerable <TProperty> foreignSource)
            where TProperty : class
            where T : class
        {
            if (source as System.Data.Entity.Infrastructure.DbQuery <T> != null)
            {
                return(System.Data.Entity.QueryableExtensions.Include(source, includePath));
            }

            Action <T, TProperty> setter;

            var foreignKeyPredicate = TestRepositoryHelper.GetForeinKeyPredicate(includePath, out setter).Compile();

            var query2 = source.AsEnumerable()
                         .Select(item =>
            {
                var include = foreignSource.FirstOrDefault(foreign =>
                {
                    return(foreignKeyPredicate(item, foreign));
                });

                if (include != null)
                {
                    setter(item, include);
                }

                return(item);
            });

            return(query2.AsQueryable());
        }