/// <summary> /// 将给定实体从基础上下文中删除,当调用 SubmitChanges 时,会将该实体从数据库中删除 /// </summary> /// <typeparam name="T">给定实体的类型</typeparam> /// <param name="TEntity">要删除的实体</param> public virtual void Delete <T>(T TEntity) { IDbQueryable <T> query = this.GetTable <T>(); query = query.CreateQuery <T>(DbExpressionType.Delete, Expression.Constant(TEntity)); _dbQueryables.Add(query); }
/// <summary> /// 根据键按升序对序列的元素排序 /// </summary> public static IDbQueryable <TSource> OrderBy <TSource>(this IDbQueryable <TSource> source, string ordering) { if (string.IsNullOrEmpty(ordering)) { return(source); } // a.BuyDate ASC string[] syntaxes = ordering.Split(' '); string[] segs = syntaxes[0].Split('.'); ParameterExpression parameter = Expression.Parameter(typeof(TSource), segs[0]); MemberExpression member = Expression.Property(parameter, segs[1]); LambdaExpression lambda = Expression.Lambda(member, parameter); DbExpressionType d = DbExpressionType.OrderBy; if (syntaxes.Length > 1 && (syntaxes[1] ?? string.Empty).ToUpper() == "DESC") { d = DbExpressionType.OrderByDescending; } return(source.CreateQuery <TSource>(d, lambda)); //source.DbExpressions.Add(new DbExpression(d, lambda)); //return source; }
/// <summary> /// 根据键按升序对序列的元素排序 /// </summary> public static IDbQueryable <TSource> OrderBy <TSource>(this IDbQueryable <TSource> source, string order) { if (string.IsNullOrEmpty(order)) { return(source); } // a.Product.BuyDate ASC string[] syntaxes = order.Split(' '); string[] segs = syntaxes[0].Split('.'); if (segs.Length <= 1) { return(source); } ParameterExpression parameter = Expression.Parameter(typeof(TSource), segs[0]); Expression node = parameter; for (int i = 1; i < segs.Length; i++) { node = Expression.Property(node, segs[i]); } LambdaExpression lambda = Expression.Lambda(node, parameter); DbExpressionType d = DbExpressionType.OrderBy; if (syntaxes.Length > 1 && (syntaxes[1] ?? string.Empty).ToUpper() == "DESC") { d = DbExpressionType.OrderByDescending; } return(source.CreateQuery <TSource>(d, lambda)); }
/// <summary> /// 将序列的每个元素投影并将结果序列组合为一个序列 /// </summary> public static IDbQueryable <TResult> SelectMany <TSource, TCollection, TResult>(this IDbQueryable <TSource> source, Expression <Func <TSource, IDbQueryable <TCollection> > > collectionSelector, Expression <Func <TSource, TCollection, TResult> > resultSelector) { return(source.CreateQuery <TResult>(new DbExpression { DbExpressionType = DbExpressionType.SelectMany, Expressions = new Expression[] { collectionSelector, resultSelector } })); }
/// <summary> /// 基于匹配键对两个序列的元素进行关联。使用默认的相等比较器对键进行比较 /// </summary> public static IDbQueryable <TResult> Join <TOuter, TInner, TKey, TResult>(this IDbQueryable <TOuter> outer, IDbQueryable <TInner> inner, Expression <Func <TOuter, TKey> > outerKeySelector, Expression <Func <TInner, TKey> > innerKeySelector, Expression <Func <TOuter, TInner, TResult> > resultSelector) { return(outer.CreateQuery <TResult>(new DbExpression { DbExpressionType = DbExpressionType.Join, Expressions = new Expression[] { Expression.Constant(inner), outerKeySelector, innerKeySelector, resultSelector } })); }
/// <summary> /// 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值 /// </summary> public static IDbQueryable <System.Linq.IGrouping <TKey, TElement> > GroupBy <TSource, TKey, TElement>(this IDbQueryable <TSource> source, Expression <Func <TSource, TKey> > keySelector, Expression <Func <TSource, TElement> > elementSelector) { return(source.CreateQuery <System.Linq.IGrouping <TKey, TElement> >(new DbExpression { DbExpressionType = DbExpressionType.GroupBy, Expressions = new Expression[] { keySelector, elementSelector } })); }
/// <summary> /// 批量新增记录 /// </summary> public void Insert<T>(IDbQueryable<T> query) { //IDbQueryable<T> source = query; //source.DbExpressions.Add(new DbExpression(DbExpressionType.Insert)); query = query.CreateQuery<T>(new DbExpression(DbExpressionType.Insert)); lock (this.oLock) _dbQueryables.Add(query); }
/// <summary> /// 新增记录 /// </summary> public virtual void Insert <T>(T TEntity) { IDbQueryable <T> query = this.GetTable <T>(); query = query.CreateQuery <T>(DbExpressionType.Insert, Expression.Constant(TEntity)); lock (this._oLock) _dbQueryables.Add(query); }
/// <summary> /// 更新记录 /// </summary> public virtual void Update <T, TFrom1, TFrom2, TFrom3>(Expression <Func <T, TFrom1, TFrom2, TFrom3, T> > action, IDbQueryable <T> query) { query = query.CreateQuery <T>(new DbExpression { DbExpressionType = DbExpressionType.Update, Expressions = new[] { action } }); lock (this._oLock) _dbQueryables.Add(query); }
/// <summary> /// 根据键按升序对序列的元素排序 /// </summary> public static IDbQueryable <TSource> OrderBy <TSource, TKey>(this IDbQueryable <TSource> source, Expression <Func <TSource, TKey> > keySelector, string order) { if (string.IsNullOrEmpty(order)) { order = "ASC"; } DbExpressionType t = order == "ASC" ? DbExpressionType.OrderBy : DbExpressionType.OrderByDescending; return(source.CreateQuery <TSource>(t, keySelector)); }
/// <summary> /// 更新记录 /// </summary> protected void Update <T>(Expression updateExpression, IDbQueryable <T> query) { query = query.CreateQuery <T>(new DbExpression { DbExpressionType = DbExpressionType.Update, Expressions = new[] { updateExpression } }); lock (this._oLock) _dbQueryables.Add(query); }
/// <summary> /// 批量将给定实体集合添加到基础上下文中,当调用 SubmitChanges 时,会将该实体插入到数据库中 /// </summary> /// <typeparam name="T">给定实体的类型</typeparam> /// <param name="collection">要添加的实体集合</param> /// <param name="entityColumns">指定插入的列</param> public virtual void Insert <T>(IEnumerable <T> collection, IList <Expression> entityColumns) { List <IDbQueryable> bulkList = new List <IDbQueryable>(); foreach (T value in collection) { IDbQueryable <T> query = this.GetTable <T>(); var expressions = entityColumns != null ? new[] { Expression.Constant(value), Expression.Constant(entityColumns) } : new[] { Expression.Constant(value) }; query = query.CreateQuery <T>(new DbExpression(DbExpressionType.Insert, expressions)); bulkList.Add(query); } _dbQueryables.Add(bulkList); }
/// <summary> /// 根据键按升序对序列的元素排序 /// </summary> public static IDbQueryable <TSource> OrderBy <TSource, TKey>(this IDbQueryable <TSource> source, Expression <Func <TSource, TKey> > keySelector) { return(source.CreateQuery <TSource>(DbExpressionType.OrderBy, keySelector)); //source.DbExpressions.Add(new DbExpression(DbExpressionType.OrderBy, keySelector)); //return source; }
/// <summary> /// 返回泛型 IDbQueryable<TResult> 中的所有值之和 /// </summary> public static TResult Sum <TSource, TResult>(this IDbQueryable <TSource> source, Expression <Func <TSource, TResult> > selector) { IDbQueryable <TResult> query = source.CreateQuery <TResult>(DbExpressionType.Sum, selector); return(query.DbContext.Database.Execute(query)); }
/// <summary> /// 强制使用嵌套查询 /// </summary> public static IDbQueryable <TSource> AsSubQuery <TSource>(this IDbQueryable <TSource> source) { return(source.CreateQuery <TSource>(DbExpressionType.AsSubQuery)); }
/// <summary> /// 指示查询应该包含外键 /// </summary> public static IDbQueryable <TResult> Include <TResult, TProperty>(this IDbQueryable <TResult> source, Expression <Func <TResult, TProperty> > path) { return(source.CreateQuery <TResult>(DbExpressionType.Include, path)); }
/// <summary> /// 返回序列中满足指定条件的第一个元素,如果未找到这样的元素,则返回默认值 /// </summary> public static TSource FirstOrDefault <TSource>(this IDbQueryable <TSource> source, Expression <Func <TSource, bool> > predicate = null) { IDbQueryable <TSource> query = source.CreateQuery <TSource>(DbExpressionType.FirstOrDefault, predicate); return(query.DbContext.Database.Execute(query)); }
/// <summary> /// 通过使用默认的相等比较器对值进行比较返回序列中的非重复元素 /// </summary> public static IDbQueryable <TSource> Distinct <TSource>(this IDbQueryable <TSource> source) { return(source.CreateQuery <TSource>(DbExpressionType.Distinct)); }
/// <summary> /// 基于谓词筛选值序列。将在谓词函数的逻辑中使用每个元素的索引 /// </summary> public static IDbQueryable <TSource> Where <TSource>(this IDbQueryable <TSource> source, Expression <Func <TSource, bool> > predicate) { return(source.CreateQuery <TSource>(DbExpressionType.Where, predicate)); }
/// <summary> /// 通过使用默认的相等比较器生成两个序列的并集。 /// </summary> public static IDbQueryable <TSource> Union <TSource>(this IDbQueryable <TSource> source, IDbQueryable <TSource> u) { return(source.CreateQuery <TSource>(DbExpressionType.Union, Expression.Constant(u))); }
/// <summary> /// 通过合并元素的索引将序列的每个元素投影到新表中 /// </summary> public static IDbQueryable <TResult> Select <TSource, TResult>(this IDbQueryable <TSource> source, Expression <Func <TSource, TResult> > selector) { return(source.CreateQuery <TResult>(DbExpressionType.Select, selector)); }
/// <summary> /// 删除记录 /// </summary> public void Delete <T>(IDbQueryable <T> query) { query = query.CreateQuery <T>(new DbExpression(DbExpressionType.Delete)); lock (this._oLock) _dbQueryables.Add(query); }
/// <summary> /// 批量新增记录 /// </summary> public virtual void Insert <T>(IDbQueryable <T> query) { query = query.CreateQuery <T>(new DbExpression(DbExpressionType.Insert)); lock (this._oLock) _dbQueryables.Add(query); }
/// <summary> /// 更新记录 /// </summary> protected void Update <T>(Expression updateExpression, IDbQueryable <T> source) { source = source.CreateQuery <T>(new DbExpression(DbExpressionType.Update, updateExpression)); lock (this._oLock) _dbQueryables.Add(source); }
/// <summary> /// 从序列的开头返回指定数量的连续元素 /// </summary> public static IDbQueryable <TSource> Take <TSource>(this IDbQueryable <TSource> source, int count) { return(source.CreateQuery <TSource>(DbExpressionType.Take, Expression.Constant(count))); }
/// <summary> /// 确定序列是否包含任何元素 /// </summary> public static bool Any <TSource>(this IDbQueryable <TSource> source, Expression <Func <TSource, bool> > predicate) { IDbQueryable <bool> query = source.CreateQuery <bool>(DbExpressionType.Any, predicate); return(query.DbContext.Database.Execute(query)); }
/// <summary> /// 根据某个键按降序对序列中的元素执行后续排序 /// </summary> public static IDbQueryable <TSource> ThenByDescending <TSource, TKey>(this IDbQueryable <TSource> source, Expression <Func <TSource, TKey> > keySelector) { return(source.CreateQuery <TSource>(DbExpressionType.ThenByDescending, keySelector)); }
/// <summary> /// 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值 /// </summary> public static IDbQueryable <System.Linq.IGrouping <TKey, TSource> > GroupBy <TSource, TKey>(this IDbQueryable <TSource> source, Expression <Func <TSource, TKey> > keySelector) { return(source.CreateQuery <System.Linq.IGrouping <TKey, TSource> >(new DbExpression(DbExpressionType.GroupBy, keySelector))); }
/// <summary> /// 返回指定序列中满足条件的元素数量 /// </summary> public static async Task <int> CountAsync <TSource>(this IDbQueryable <TSource> source, Expression <Func <TSource, bool> > predicate) { IDbQueryable <int> query = source.CreateQuery <int>(DbExpressionType.Count, predicate); return(await query.DbContext.Database.ExecuteAsync(query)); }
/// <summary> /// 返回指定序列的元素;如果序列为空,则返回单一实例集合中的类型参数的默认值 /// </summary> /// <param name="source">查询语义</param> /// <param name="g">是否为右联</param> /// <returns></returns> public static IDbQueryable <TSource> DefaultIfEmpty <TSource>(this IDbQueryable <TSource> source, bool g) { return(source.CreateQuery <TSource>(DbExpressionType.DefaultIfEmpty)); }