コード例 #1
0
        public Task <IQueryable> QueryAsync(Type type, Expression <Func <object, bool> > predicate)
        {
            var newPredicate = ChangeInputType(predicate, type);

            var dbContext = new AmbientDbContextLocator().Get <TDbContext>(); // scope.DbContexts.Get<TDbContext>();

            var dbContextType = dbContext.GetType();

            var setMethodGeneric = dbContextType.GetTypeInfo().GetRuntimeMethod(nameof(DbContext.Set), Array.Empty <Type>());

            var setMethod = setMethodGeneric.MakeGenericMethod(type);

            var set = (IQueryable)setMethod.Invoke(dbContext, Array.Empty <object>());

            var whereMethod = typeof(Queryable).GetRuntimeMethods()
                              .Where(x =>
                                     x.Name == nameof(Queryable.Where))
                              .First(x =>
                                     x.GetParameters()[1].ParameterType.GetGenericArguments()[0].GetGenericArguments().Length == 2);

            var whereTyped = whereMethod.MakeGenericMethod(type);

            var results = (IQueryable)whereTyped.Invoke(null, new object[] { set, newPredicate });

            return(Task.FromResult(results));
        }