public MongoDBRepositoryProvider(IContextService service) { var context = service.InitializeContext; var connectionString = context.ConnectionString; var serverName = connectionString.Properties.TryGetValue("server"); var database = connectionString.Properties.TryGetValue("database"); if (string.IsNullOrEmpty(database)) { database = "admin"; } var client = new MongoClient(serverName); var server = client.GetServer(); var db = server.GetDatabase(database); var metadata = EntityMetadataUnity.GetEntityMetadata(typeof(TEntity)); var collectionSettings = new MongoCollectionSettings { AssignIdOnInsert = false }; collection = db.GetCollection <TEntity>(metadata.TableName, collectionSettings); BsonSerializer.RegisterSerializer(new CustomBsonSerializer()); var provider = new MongoQueryProvider(collection); QueryProvider = provider; Queryable = new MongoQueryable <TEntity>(provider); }
public async Task DeleteManyAsync(Expression <Func <TEntity, bool> > predicate, bool autoSave = false, CancellationToken cancellationToken = default) { var entities = await MongoQueryable.Where((await GetMongoQueryableAsync(cancellationToken)), predicate) .ToListAsync(cancellationToken); await DeleteManyAsync(entities, autoSave, cancellationToken); }
public async Task <StaticPagedList <T> > PaginationAsync <TKey>(int pageIndex, int pageSize, Expression <Func <T, TKey> > orderBy, bool isOrderByAsc = true, Expression <Func <T, bool> > where = null) { long num; long num1 = (long)0; IMongoQueryable <T> mongoQueryable = IMongoCollectionExtensions.AsQueryable <T>(this.Collection, new AggregateOptions() { AllowDiskUse = true }); num = (where != null ? await this.CountAsync(where) : await this.CountAsync()); num1 = num; if (where != null) { mongoQueryable = MongoQueryable.Where <T>(mongoQueryable, where); } if (isOrderByAsc) { mongoQueryable = MongoQueryable.Take <T>(MongoQueryable.Skip <T>(MongoQueryable.OrderBy <T, TKey>(mongoQueryable, orderBy), (pageIndex - 1) * pageSize), pageSize); } else { mongoQueryable = MongoQueryable.Take <T>(MongoQueryable.Skip <T>(MongoQueryable.OrderByDescending <T, TKey>(mongoQueryable, orderBy), (pageIndex - 1) * pageSize), pageSize); } StaticPagedList <T> staticPagedList = new StaticPagedList <T>(mongoQueryable.ToList <T>(), pageIndex, pageSize, (int)num1); return(staticPagedList); }
public async Task <List <TEntity> > GetListAsync(Expression <Func <TEntity, bool> > predicate, bool includeDetails = true, CancellationToken cancellationToken = default) { var query = await GetMongoQueryableAsync(cancellationToken); return(await MongoQueryable.Where(query, predicate) .ToListAsync(cancellationToken)); }
internal static IQueryable <T> ThenByDescending <T, TKey>(this IOrderedQueryable <T> queryable, Expression <Func <T, TKey> > filter) { if (queryable is IOrderedMongoQueryable <T> mongoQueryable) { return(MongoQueryable.ThenByDescending(mongoQueryable, filter)); } return(Queryable.ThenByDescending(queryable, filter)); }
internal static IQueryable <T> OrderBy <T, TKey>(this IQueryable <T> queryable, Expression <Func <T, TKey> > filter) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.OrderBy(mongoQueryable, filter)); } return(Queryable.OrderBy(queryable, filter)); }
internal static IQueryable <T> Where <T>(this IQueryable <T> queryable, Expression <Func <T, bool> > filter) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.Where(mongoQueryable, filter)); } return(Queryable.Where(queryable, filter)); }
internal static Task <int> CountAsync <T>(this IQueryable <T> queryable) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.CountAsync(mongoQueryable)); } return(Task.FromResult(Queryable.Count(queryable))); }
internal static Task <T?> SingleOrDefaultAsync <T>(this IQueryable <T> queryable) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.SingleOrDefaultAsync(mongoQueryable)); } return(Task.FromResult(Queryable.SingleOrDefault(queryable))); }
internal static IQueryable <TResult> SelectMany <T, TResult>(this IQueryable <T> queryable, Expression <Func <T, IEnumerable <TResult> > > filter) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.SelectMany(mongoQueryable, filter)); } return(Queryable.SelectMany(queryable, filter)); }
internal static Task <TResult> MaxAsync <T, TResult>(this IQueryable <T> queryable, Expression <Func <T, TResult> > filter) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.MaxAsync(mongoQueryable, filter)); } return(Task.FromResult(Queryable.Max(queryable, filter))); }
internal static IQueryable <T> Take <T>(this IQueryable <T> queryable, int count) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.Take(mongoQueryable, count)); } return(Queryable.Take(queryable, count)); }
internal static IQueryable <IGrouping <TKey, T> > GroupBy <T, TKey>(this IQueryable <T> queryable, Expression <Func <T, TKey> > filter) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.GroupBy(mongoQueryable, filter)); } return(Queryable.GroupBy(queryable, filter)); }
internal static IQueryable <TResult> OfType <TResult>(this IQueryable queryable) { if (queryable is IMongoQueryable mongoQueryable) { return(MongoQueryable.OfType <TResult>(mongoQueryable)); } return(Queryable.OfType <TResult>(queryable)); }
internal static Task <int> CountAsync <T>(this IQueryable <T> queryable, Expression <Func <T, bool> > filter) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.CountAsync(mongoQueryable, filter)); } return(Task.FromResult(Queryable.Count(queryable, filter))); }
internal static IQueryable <T> Distinct <T>(this IQueryable <T> queryable) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(MongoQueryable.Distinct(mongoQueryable)); } return(Queryable.Distinct(queryable)); }
public static async Task <T> SingleOrDefaultAsync <T>(this IQueryable <T> queryable) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(await MongoQueryable.SingleOrDefaultAsync(mongoQueryable)); } else { return(queryable.SingleOrDefault()); } }
public static async Task <bool> AnyAsync <T>(this IQueryable <T> queryable, Expression <Func <T, bool> > predicate) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(await MongoQueryable.AnyAsync(mongoQueryable, predicate)); } else { return(queryable.Any(predicate)); } }
public static async Task <int> CountAsync <T>(this IQueryable <T> queryable) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(await MongoQueryable.CountAsync(mongoQueryable)); } else { return(queryable.Count()); } }
public static async Task <bool> AnyAsync <T>(this IQueryable <T> queryable) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(await MongoQueryable.AnyAsync(mongoQueryable)); } else { return(queryable.Any()); } }
public static async Task <T> SingleOrDefaultAsync <T>(this IQueryable <T> queryable, Expression <Func <T, bool> > predicate) { if (queryable is IMongoQueryable <T> mongoQueryable) { return(await MongoQueryable.SingleOrDefaultAsync(mongoQueryable, predicate)); } else { return(queryable.SingleOrDefault(predicate)); } }
public StaticPagedList <T> Pagination <TKey>(int page, int total, Expression <Func <T, TKey> > orderBy, Expression <Func <T, bool> > where = null) { long count = (long)0; IMongoQueryable <T> model = IMongoCollectionExtensions.AsQueryable <T>(this.Collection); count = (where == null ? this.Count() : this.Count(where)); if (where != null) { model = MongoQueryable.Where <T>(model, where); } model = MongoQueryable.Take <T>(MongoQueryable.Skip <T>(MongoQueryable.OrderBy <T, TKey>(model, orderBy), (page - 1) * total), total); return(new StaticPagedList <T>(model.ToList <T>(), page, total, (int)count)); }
public StaticPagedList <T> Pagination <TKey>(int pageIndex, int pageSize, Expression <Func <T, TKey> > orderBy, bool isOrderByAsc = true, Expression <Func <T, bool> > where = null) { long count = (long)0; IMongoQueryable <T> model = IMongoCollectionExtensions.AsQueryable <T>(this.Collection); count = (where == null ? this.Count() : this.Count(where)); if (where != null) { model = MongoQueryable.Where <T>(model, where); } model = MongoQueryable.Take <T>(MongoQueryable.Skip <T>(isOrderByAsc ? MongoQueryable.OrderBy <T, TKey>(model, orderBy) : MongoQueryable.OrderByDescending <T, TKey>(model, orderBy), (pageIndex - 1) * pageSize), pageSize); return(new StaticPagedList <T>(model.ToList <T>(), pageIndex, pageSize, (int)count)); }
public IMongoQueryable <T> Query(params Expression <Func <T, bool> >[] where) { IMongoQueryable <T> _query = this.Query(); if (where.Count <Expression <Func <T, bool> > >() > 0) { Expression <Func <T, bool> >[] expressionArray = where; for (int i = 0; i < (int)expressionArray.Length; i++) { _query = MongoQueryable.Where <T>(_query, expressionArray[i]); } } return(_query); }
public MongoDBRepositoryProvider(InternalContext context) { var connectionString = context.Database.ConnectionString; var client = new MongoClient((string)connectionString); var db = client.GetDatabase(connectionString.Properties.TryGetValue("database")); var metadata = EntityMetadataUnity.GetEntityMetadata(typeof(TEntity)); var collectionSettings = new MongoCollectionSettings { AssignIdOnInsert = false }; collection = (MongoCollection <TEntity>)db.GetCollection <TEntity>(metadata.TableName, collectionSettings); var provider = new MongoQueryProvider(collection); QueryProvider = provider; Queryable = new MongoQueryable <TEntity>(provider); }
public async Task <StaticPagedList <T> > PaginationAsync <TKey>(int page, int total, Expression <Func <T, TKey> > orderBy, Expression <Func <T, bool> > where = null) { long num; long num1 = (long)0; IMongoQueryable <T> mongoQueryable = IMongoCollectionExtensions.AsQueryable <T>(this.Collection); num = (where != null ? await this.CountAsync(where) : await this.CountAsync()); num1 = num; if (where != null) { mongoQueryable = MongoQueryable.Where <T>(mongoQueryable, where); } mongoQueryable = MongoQueryable.Take <T>(MongoQueryable.Skip <T>(MongoQueryable.OrderBy <T, TKey>(mongoQueryable, orderBy), (page - 1) * total), total); StaticPagedList <T> staticPagedList = new StaticPagedList <T>(mongoQueryable.ToList <T>(), page, total, (int)num1); return(staticPagedList); }
public static IMongoQuery ToMongoQuery <T>(this IEnumerable <T> query) { MongoQueryable <T> mongoQuery = query as MongoQueryable <T>; if (mongoQuery != null) { return(mongoQuery.GetMongoQuery()); } MongoCursor <T> mongoCursor = query as MongoCursor <T>; if (mongoCursor != null) { return(mongoCursor.Query); } return(null); }
public static IMongoQuery ToMongoQuery <T>(this IEnumerable <T> query) { MongoQueryable <T> mongoQuery = query as MongoQueryable <T>; if (mongoQuery != null) { return(mongoQuery.GetMongoQuery()); } MongoCursor <T> mongoCursor = query as MongoCursor <T>; if (mongoCursor != null) { return(mongoCursor.Query); } string msg = string.Format("Cannot convert from {0} to either {1} or {2}.", query.GetType().Name, typeof(MongoQueryable <T>).Name, typeof(MongoCursor <T>).Name); throw new ArgumentException(msg); }
/// <summary> /// 初始化 <see cref="MongoDBRepositoryProvider{TEntity}"/> 类的新实例。 /// </summary> /// <param name="contextService"></param> public MongoDBRepositoryProvider(MongoDBContextService contextService) { _contextService = contextService; var metadata = EntityMetadataUnity.GetEntityMetadata(typeof(TEntity)); var collectionSettings = new MongoCollectionSettings { AssignIdOnInsert = false }; _collection = contextService.Database.GetCollection <TEntity>(metadata.TableName, collectionSettings); _cache.GetOrAdd(typeof(TEntity), contextService.ContextType, (k, c) => { var serializer = new CustomBsonSerializer(c); BsonSerializer.RegisterSerializer(serializer); return(serializer); }); var provider = new MongoQueryProvider(_collection); QueryProvider = provider; Queryable = new MongoQueryable <TEntity>(provider); }
public IMongoQueryable <T> Query <TKey>(Expression <Func <T, TKey> > orderBy, params Expression <Func <T, bool> >[] where) { return(MongoQueryable.OrderBy <T, TKey>(this.Query(where), orderBy)); }