예제 #1
0
        public static async Task LoadCollectionPropertyAsync(this DbContext context, object entity, string collectionExpression, string search = "", string orderBy = null, bool ascending = false, int?skip = null, int?take = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            string collectionProperty = RelationshipHelper.GetCollectionExpressionCurrentCollection(collectionExpression, entity.GetType());
            object collectionItemId   = RelationshipHelper.GetCollectionExpressionCurrentCollectionItem(collectionExpression);

            var collectionItemType = entity.GetType().GetGenericArguments(collectionProperty).Single();

            Type iQueryableType = typeof(IQueryable <>).MakeGenericType(new[] { collectionItemType });

            var query = context.Entry(entity)
                        .Collection(collectionProperty)
                        .Query();

            if (collectionItemId != null)
            {
                var whereClause = Utilities.SearchForEntityById(collectionItemType, collectionItemId);
                typeof(LamdaHelper).GetMethod(nameof(LamdaHelper.Where)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, whereClause });
            }
            else
            {
                if (!string.IsNullOrEmpty(search))
                {
                    query = (IQueryable)typeof(Utilities).GetMethod(nameof(Utilities.CreateSearchQuery)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, search });
                }

                if (!string.IsNullOrWhiteSpace(orderBy))
                {
                    query = (IQueryable)typeof(Utilities).GetMethod(nameof(Utilities.QueryableOrderBy)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, orderBy, ascending });
                }

                if (skip.HasValue)
                {
                    typeof(Queryable).GetMethod(nameof(System.Linq.Queryable.Skip)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, skip.Value });
                }

                if (take.HasValue)
                {
                    typeof(Queryable).GetMethod(nameof(System.Linq.Queryable.Take)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, take.Value });
                }
            }

            await((Task)(typeof(EntityFrameworkQueryableExtensions).GetMethod(nameof(EntityFrameworkQueryableExtensions.LoadAsync)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, cancellationToken }))).ConfigureAwait(false);

            if (collectionItemId != null && RelationshipHelper.CollectionExpressionHasMoreCollections(collectionExpression))
            {
                var items = entity.GetPropValue(collectionProperty) as IEnumerable;
                if (items != null)
                {
                    foreach (var item in items)
                    {
                        await context.LoadCollectionPropertyAsync(item, RelationshipHelper.GetCollectionExpressionNextCollection(collectionExpression), search, orderBy, ascending, skip, take);
                    }
                }
            }
        }
예제 #2
0
        public static async Task <int> CollectionPropertyCountAsync(this DbContext context, object entity, string collectionExpression, string search, CancellationToken cancellationToken)
        {
            string collectionProperty = RelationshipHelper.GetCollectionExpressionCurrentCollection(collectionExpression, entity.GetType());

            var collectionItemType = entity.GetType().GetGenericArguments(collectionProperty).Single();

            Type iQueryableType = typeof(IQueryable <>).MakeGenericType(new[] { collectionItemType });

            var query = context.Entry(entity)
                        .Collection(collectionProperty)
                        .Query();

            if (!string.IsNullOrEmpty(search))
            {
                query = (IQueryable)typeof(Utilities).GetMethod(nameof(Utilities.CreateSearchQuery)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, search });
            }

            return(await((Task <int>)(typeof(Utilities).GetMethod(nameof(Utilities.CountEFCoreAsync)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, cancellationToken }))).ConfigureAwait(false));
        }
예제 #3
0
        public static int CollectionPropertyCount(this DbContext context, object entity, string collectionExpression, string search = "")
        {
            string collectionProperty = RelationshipHelper.GetCollectionExpressionCurrentCollection(collectionExpression, entity.GetType());

            var collectionItemType = entity.GetType().GetGenericArguments(collectionProperty).Single();

            Type iQueryableType = typeof(IQueryable <>).MakeGenericType(new[] { collectionItemType });

            var query = context.Entry(entity)
                        .Collection(collectionProperty)
                        .Query();

            if (!string.IsNullOrEmpty(search))
            {
                query = (IQueryable)typeof(Utilities).GetMethod(nameof(Utilities.CreateSearchQuery)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query, search });
            }

            return((int)(typeof(Utilities).GetMethod(nameof(Utilities.Count)).MakeGenericMethod(collectionItemType).Invoke(null, new object[] { query })));
        }