/// <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()); }
/// <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()); }
/// <summary> /// Returns list of constructed database identity generation action /// </summary> /// <returns>list of actions</returns> internal static IList <DbGeneratedIdentityAction> GetDatabaseGeneratedIdentitys() { var type = typeof(T); var dbGeneratedActions = new List <DbGeneratedIdentityAction>(); var properties = type.GetProperties().FilterPropertiesByAttribute <DatabaseGeneratedAttribute, PropertyInfo>( (attr, prop) => attr.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity, (attr, prop) => prop ); DatabaseGeneratedAttribute addAttribute = null; var propertiesKeys = type.GetProperties().FilterPropertiesByAttribute <KeyAttribute, PropertyInfo>( (attr, prop) => (addAttribute = prop.GetCustomAttribute <DatabaseGeneratedAttribute>()) == null || addAttribute.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity, (attr, prop) => prop ).ToList(); if (propertiesKeys.Count > 1) { propertiesKeys = new List <PropertyInfo>(1); } properties = properties.Union(propertiesKeys); foreach (var prop in properties) { var act = new DbGeneratedIdentityAction(TestRepositoryHelper.ConstructDbGeneratedIdentity <T>(prop.Name)); dbGeneratedActions.Add(act); } return(dbGeneratedActions); }
/// <summary> /// Returns list of constructed foreign key generation action /// </summary> /// <returns></returns> internal static IList <Action <T> > GetForeignKeyRelations() { var type = typeof(T); var foreignKeyActions = new List <Action <T> >(); foreach (var prop in type.GetProperties().FilterPropertiesByAttribute <ForeignKeyAttribute>()) { var attr = prop.GetCustomAttribute <ForeignKeyAttribute>(); var act = TestRepositoryHelper.ConstructForeignKeyTrigger <T>(prop.Name, attr.Name); foreignKeyActions.Add(act); } return(foreignKeyActions); }
/// <summary> /// Returns list of max length constraint actions /// </summary> /// <returns>list of actions</returns> internal static IList <Action <T> > GetMinLengthConstraintActions() { var type = typeof(T); var minLengthConstraintActions = new List <Action <T> >(); foreach (var prop in type.GetProperties().FilterPropertiesByAttribute <MinLengthAttribute>()) { var attr = prop.GetCustomAttribute <MinLengthAttribute>(); var act = TestRepositoryHelper.ConstructMinLengthConstraint <T>(prop.Name, attr.Length, attr.ErrorMessage); minLengthConstraintActions.Add(act); } return(minLengthConstraintActions); }