/// <summary> /// 是否存在该记录 /// </summary> /// <returns></returns> public bool ExistsViewEntity(Expression<Func<VCustomerContact , bool>> predicate) { using (UCDbContext db=new UCDbContext()) { bool status= db.VCustomerContacts.Any(predicate); return status; } }
/// <summary> /// 是否存在该记录 /// </summary> /// <returns></returns> public bool ExistsViewEntity(Expression<Func<VSysDepartment , bool>> predicate) { using (UCDbContext db=new UCDbContext()) { bool status= db.VSysDepartments.Any(predicate); return status; } }
/// <summary> /// 是否存在该记录 /// </summary> /// <returns></returns> public bool ExistsViewEntity(Expression<Func<VCompanyUser , bool>> predicate) { using (UCDbContext db=new UCDbContext()) { bool status= db.VCompanyUsers.Any(predicate); return status; } }
/// <summary> /// 获取数据总数 /// </summary> /// <returns>返回所有数据总数</returns> public int GetViewCount() { using (UCDbContext db=new UCDbContext()) { var models= db.VCompanyUsers; var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models.Count(); } }
/// <summary> /// 获取数据总数 /// </summary> /// <param name="predicate">Lamda表达式</param> /// <returns>返回所有数据总数</returns> public int GetViewCount(Expression<Func<VCompanyUser, bool>> predicate) { using (UCDbContext db=new UCDbContext()) { var models= db.VCompanyUsers.Where<VCompanyUser>(predicate); var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models.Count(); } }
/// <summary> /// 获取指定的单个实体 /// 如果不存在则返回null /// 如果存在多个则抛异常 /// </summary> /// <param name="predicate">Lamda表达式</param> /// <returns>Entity</returns> public VCompanyUser GetViewEntity(Expression<Func<VCompanyUser, bool>> predicate) { using (UCDbContext db=new UCDbContext()) { var model =db.VCompanyUsers.Where<VCompanyUser>(predicate); var sqlText = model.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return model.SingleOrDefault(); } }
/// <summary> /// 删除实体 /// </summary> /// <param name="predicate">Lamda表达式</param> public bool DeleteEntity(Expression<Func<TFunMyapp , bool>> predicate) { using (UCDbContext db=new UCDbContext()) { TFunMyapp entity = db.TFunMyapps.Where(predicate).First(); int rows=db.TFunMyapps.Delete(entity); if (rows > 0) { return true; } else { return false; } } }
/// <summary> /// 批量删除(实体集合) /// </summary> /// <param name="predicate">动态LINQ</param> public bool DeletesEntity(List<string> predicates) { using (UCDbContext db=new UCDbContext()) { if (db.Connection.State != ConnectionState.Open) { db.Connection.Open(); } var tran = db.Connection.BeginTransaction(); try { foreach (var predicate in predicates) { var item = db.TFunMyapps.Where(predicate).FirstOrDefault(); if (null != item) { db.TFunMyapps.Delete(item); } } tran.Commit(); LogHelper.Debug("批量删除成功!"); return true; } catch (Exception ex) { tran.Rollback(); LogHelper.Error("批量删除异常:", ex); return false; } finally { if (db.Connection.State != ConnectionState.Closed) { db.Connection.Close(); } } } }
/// <summary> /// 获取所有的数据 /// </summary> /// <param name="predicate">Lamda表达式</param> /// <returns>返回所有数据列表</returns> public List<TFunMyapp> GetList(Expression<Func<TFunMyapp, bool>> predicate) { using (UCDbContext db=new UCDbContext()) { var models= db.TFunMyapps.Where<TFunMyapp>(predicate); var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models.ToList(); } }
/// <summary> /// 根据条件查询某些字段(LINQ 动态查询) /// </summary> /// <param name="selector">要查询的字段(格式:new(ID,Name))</param> /// <param name="predicate">筛选条件(id=0)</param> /// <returns></returns> public IQueryable<Object> GetFields(string selector, string predicate, params object[] values) { using (UCDbContext db=new UCDbContext()) { var model = db.TFunMyapps.Where(predicate,values).Select(selector); var sqlText = model.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return (IQueryable<object>) model; } }
/// <summary> /// 获取所有的数据 /// </summary> /// <returns>返回所有数据列表</returns> public List<TFunMyapp> GetList() { using (UCDbContext db=new UCDbContext()) { var models= db.TFunMyapps; var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models.ToList(); } }
/// <summary> /// 修改实体 /// </summary> /// <param name="entity">实体对象</param> public bool UpdateEntity(TSysDepartment entity) { using (UCDbContext db=new UCDbContext()) { int rows= db.TSysDepartments.Update(entity); if (rows > 0) { return true; } else { return false; } } }
/// <summary> /// 返回单个实体(带扩展字段) /// </summary> /// <param name="selector">要查询的字段</param> /// <param name="expFields">存储扩展字段值的字段</param> /// <param name="expSelector">要查询的扩展字段里面的字段</param> /// <param name="predicate">查询条件</param> /// <param name="values">参数</param> /// <returns>Dictionary<String, Object>.</returns> public Dictionary<string, object> GetEntityWithExp(string selector, string expFields, string expSelector, string predicate, params object[] values) { using (UCDbContext db=new UCDbContext()) { IQueryable<object> model=null; if (!String.IsNullOrEmpty(selector)) { model = ((IQueryable<object>) db.TFunMyapps.Where(predicate, values).Select(selector, values)); } else { model = ((IQueryable<object>)db.TFunMyapps.Where(predicate, values)); } object temp = model.FirstOrDefault(); var sqlText = model.GetProperty("SqlText"); LogHelper.Debug("ELINQ Paging:<br/>" + sqlText.ToString()); Dictionary<string, object> result = new Dictionary<string, object>(); PropertyInfo[] propertyInfos = temp.GetType().GetProperties(); foreach (var propertyInfo in propertyInfos) { var name = propertyInfo.Name; //输入的selector中的字段名 var value = propertyInfo.GetValue(temp, null); //输入的selector中的字段值 if (name == expFields) { IDictionary<string, JToken> cusFields = (JObject) JsonConvert.DeserializeObject(value.ToString()); //循环添加新字段 foreach (var field in cusFields) { //只输出已选择的扩展字段(不输出直接留空) if (!String.IsNullOrEmpty(expSelector)) { object[] exps = Utils.StringToObjectArray(expSelector, ','); var fieldKey = NamingConversion.Default.PropertyName(field.Key); var fieldvalue = field.Value; if (exps.Contains(fieldKey)) //只查询选择的扩展字段 { result.Add(fieldKey, fieldvalue); } } } } else { result.Add(name, value); } } return result; } }
/// <summary> /// 获取最大Id(默认) /// </summary> /// <returns></returns> public int GetMaxId() { using (UCDbContext db=new UCDbContext()) { var models = db.TFunMyapps.Max(temp => temp.Id); var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models + 1; } }
/// <summary> /// 使用LINQ批量更改TClientInfo状态 2014-09-05 14:58:50 By 唐有炜 /// </summary> /// <param name="fields">要更新的字段(支持批量更新)</param> /// <param name="values">The values.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool UpdateEntityStatus(List<Field> fields, params object[] values) { using (UCDbContext db=new UCDbContext()) { if (db.Connection.State != ConnectionState.Open) { db.Connection.Open(); } var tran = db.Connection.BeginTransaction(); try { foreach (var field in fields) { var entity = db.TFunMyapps.Where(field.Predicate, values).FirstOrDefault(); var propertyInfos = entity.GetType().GetProperties(); foreach (var p in propertyInfos) { if (p.Name == field.Key) { p.SetValue(entity, field.Value, null); //给对应属性赋值 } } db.TFunMyapps.Update(entity); } tran.Commit(); LogHelper.Debug("TFunMyapp字段批量更新成功。"); return true; } catch (Exception ex) { tran.Rollback(); LogHelper.Error("TFunMyapp字段批量更新异常:", ex); return false; } finally { if (db.Connection.State != ConnectionState.Closed) { db.Connection.Close(); } } } }
/// <summary> /// 查询分页(包括扩展字段) 2014-08-29 14:58:50 By 唐有炜 /// </summary> /// <param name="pageIndex">页码</param> /// <param name="pageSize">每页的数目</param> /// <param name="selector">要查询的字段</param> /// <param name="expFields">存储扩展字段值的字段</param> /// <param name="expSelector">要查询的扩展字段里面的字段</param> /// <param name="predicate">查询条件</param> /// <param name="ordering">排序</param> /// <param name="recordCount">记录结果数</param> /// <param name="values">参数</param> /// <returns>查询分页(包括扩展字段)</returns> public List<Dictionary<string, object>> GetListWithExpByPage(int pageIndex, int pageSize, string selector, string expFields, string expSelector, string predicate, string ordering, out int recordCount, params object[] values) { using (UCDbContext db=new UCDbContext()) { //获取查询结果 //加上扩展字段值 var temps = db.TFunMyapps; recordCount = temps.Count(); var prevCount = (pageIndex - 1)*pageSize; IQueryable<object> models = null; if (!String.IsNullOrEmpty(selector)) { models = (IQueryable<object>) (temps .Skip(prevCount) .Take(pageSize) .Where(predicate, values) .Select(selector, values) .OrderBy(ordering)); } else { models = (IQueryable<object>)(temps .Skip(prevCount) .Take(pageSize) .Where(predicate, values) .OrderBy(ordering)); } var sqlText = models.GetProperty("SqlText"); LogHelper.Debug("ELINQ Dynamic Paging:<br/>" + sqlText.ToString()); //转换为分页 var pages = models.ToPagination(pageIndex, pageSize, recordCount); //组装输出 List<Dictionary<string, object>> results = new List<Dictionary<string, object>>(); foreach (var page in pages) { Dictionary<string, object> result = new Dictionary<string, object>(); PropertyInfo[] propertyInfos = page.GetType().GetProperties(); foreach (var propertyInfo in propertyInfos) { var name = propertyInfo.Name; //输入的selector中的字段名 var value = propertyInfo.GetValue(page, null); //输入的selector中的字段值 if (name == expFields) { IDictionary<string, JToken> cusFields = (JObject) JsonConvert.DeserializeObject(value.ToString()); //循环添加新字段 foreach (var field in cusFields) { //只输出已选择的扩展字段(不输出直接留空) if (!String.IsNullOrEmpty(expSelector)) { object[] exps = Utils.StringToObjectArray(expSelector, ','); var fieldKey = NamingConversion.Default.PropertyName(field.Key); var fieldvalue = field.Value; if (exps.Contains(fieldKey)) //只查询选择的扩展字段 { result.Add(fieldKey, fieldvalue); } } } } else { result.Add(name, value); } } results.Add(result); } return results; } }
/// <summary> /// 查询分页(可以自定义添加属性,不包括扩展字段) 2014-10-15 14:58:50 By 唐有炜 /// </summary> /// <param name="pageIndex">页码</param> /// <param name="pageSize">每页的数目</param> /// <param name="selector">要查询的字段</param> /// <param name="predicate">查询条件</param> /// <param name="ordering">排序</param> /// <param name="recordCount">记录结果数</param> /// <param name="values">参数</param> /// <returns>查询分页(不包括扩展字段)</returns> public List<Dictionary<string, object>> GetListByPage(int pageIndex, int pageSize, string selector,string predicate, string ordering, out int recordCount, params object[] values) { using (UCDbContext db=new UCDbContext()) { //获取查询结果 //加上扩展字段值 var temps = db.TFunMyapps; IQueryable<object> models = null; if (!String.IsNullOrEmpty(selector)) { models = (IQueryable<object>) (temps .Where(predicate, values) .Select(selector, values) .OrderBy(ordering)); } else { models = (IQueryable<object>)(temps .Where(predicate, values) .OrderBy(ordering)); } var sqlText = models.GetProperty("SqlText"); LogHelper.Debug("ELINQ Dynamic Paging:<br/>" + sqlText.ToString()); //计算总数 recordCount = models.Count(); //转换为分页 var prevCount = (pageIndex - 1)*pageSize; models=models.Skip(prevCount).Take(pageSize); var pages = models.ToPagination(pageIndex, pageSize, recordCount); //组装输出 List<Dictionary<string, object>> results = new List<Dictionary<string, object>>(); foreach (var page in pages) { Dictionary<string, object> result = new Dictionary<string, object>(); PropertyInfo[] propertyInfos = page.GetType().GetProperties(); foreach (var propertyInfo in propertyInfos) { var name = propertyInfo.Name; //输入的selector中的字段名 var value = propertyInfo.GetValue(page, null); //输入的selector中的字段值 result.Add(name, value); } results.Add(result); } return results; } }
/// <summary> /// 获取所有的数据 /// </summary> /// <returns>返回所有数据列表</returns> public List<VCustomerContact> GetViewList() { using (UCDbContext db=new UCDbContext()) { var models= db.VCustomerContacts; var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models.ToList(); } }
/// <summary> /// 获取所有的数据 /// </summary> /// <param name="predicate">Lamda表达式</param> /// <returns>返回所有数据列表</returns> public List<VCustomerContact> GetViewList(Expression<Func<VCustomerContact, bool>> predicate) { using (UCDbContext db=new UCDbContext()) { var models= db.VCustomerContacts.Where<VCustomerContact>(predicate); var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models.ToList(); } }
//以下是原生Sql方法============================================================== //=========================================================================== /// <summary> /// 用SQL语句查询 /// </summary> /// <param name="sql">sql语句</param> /// <param name="namedParameters">sql参数</param> /// <returns>集合</returns> public DataTable GetViewListBySql(string sql, dynamic namedParameters) { using (UCDbContext db=new UCDbContext()) { return db.DbHelper.ExecuteDataTable(sql,namedParameters).ToList<VCompanyUser>(); } }
//查询分页(LINQ动态) public IPagination<TFunMyapp> GetListByPage(int pageIndex, int pageSize, out int rowCount, IDictionary<string, UCsoft.Entity.UCEnums.OrderEmum> orders, string predicate) { using (UCDbContext db = new UCDbContext()) { var roles = db.TFunMyapps.Where(predicate); rowCount = roles.Count(); var prevCount = (pageIndex - 1)*pageSize; var models = roles .Skip(prevCount) .Take(pageSize); foreach (var order in orders) { models = models.OrderBy(String.Format("{0} {1}", order.Key, order.Value)); } var sqlText = models.GetProperty("SqlText"); LogHelper.Debug("ELINQ Paging:<br/>" + sqlText.ToString()); return models.ToPagination(pageSize, pageSize, rowCount); } }
/// <summary> /// 根据条件查询某些字段(LINQ 动态查询) /// </summary> /// <param name="selector">要查询的字段(格式:new(ID,Name))</param> /// <param name="predicate">筛选条件(id=0)</param> /// <returns></returns> public IQueryable<Object> GetViewFields(string selector, string predicate) { using (UCDbContext db=new UCDbContext()) { var model = db.VCompanyUsers.Where(predicate).Select(selector); var sqlText = model.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return (IQueryable<object>) model; } }
//以下是原生Sql方法============================================================== //=========================================================================== /// <summary> /// 用SQL语句查询 /// </summary> /// <param name="sql">sql语句</param> /// <param name="namedParameters">sql参数</param> /// <returns>集合</returns> public DataTable GetListBySql(string sql, dynamic namedParameters) { using (UCDbContext db=new UCDbContext()) { return db.DbHelper.ExecuteDataTable(sql,namedParameters).ToList<TFunMyapp>(); } }
/// <summary> /// 是否存在该记录 /// </summary> /// <returns></returns> public bool ExistsEntity(Expression<Func<TFunMyapp , bool>> predicate) { using (UCDbContext db=new UCDbContext()) { bool status= db.TFunMyapps.Any(predicate); return status; } }
/// <summary> /// 是否存在该记录 /// </summary> /// <returns></returns> public bool ExistsEntity(string predicate,params object[] values) { using (UCDbContext db=new UCDbContext()) { bool status= db.TFunMyapps.Where(predicate,values).Any(); return status; } }
/// <summary> /// 获取数据总数(动态LINQ) /// </summary> /// <param name="predicate">动态LINQ</param> /// <param name="values">参数</param> /// <returns>返回所有数据总数</returns> public int GetCount(string predicate,params object[] values) { using (UCDbContext db=new UCDbContext()) { var models= db.TFunMyapps.Where(predicate); var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models.Count(); } }
/// <summary> /// 修改实体 /// </summary> /// <param name="entity">实体对象</param> public bool UpdateEntity(TFunMyapp entity) { using (UCDbContext db=new UCDbContext()) { int rows= db.TFunMyapps.Update(entity); if (rows > 0) { return true; } else { return false; } } }
/// <summary> /// 获取指定的单个实体 /// 如果不存在则返回null /// 如果存在多个则抛异常 /// </summary> /// <param name="predicate">Lamda表达式</param> /// <returns>Entity</returns> public TFunMyapp GetEntity(Expression<Func<TFunMyapp, bool>> predicate) { using (UCDbContext db=new UCDbContext()) { var model =db.TFunMyapps.Where<TFunMyapp>(predicate); var sqlText = model.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return model.SingleOrDefault(); } }
/// <summary> /// 执行Sql /// </summary> /// <param name="sql">Sql语句</param> /// <param name="namedParameters">查询字符串</param> /// <returns></returns> public bool ExecuteSql(string sql, dynamic namedParameters = null) { using (UCDbContext db=new UCDbContext()) { var rows = db.DbHelper.ExecuteNonQuery(sql, namedParameters); if (rows > 0) { return true; } else { return false; } } }
/// <summary> /// 获取数据总数(LINQ) /// </summary> /// <param name="predicate">Lamda表达式</param> /// <returns>返回所有数据总数</returns> public int GetCount(Expression<Func<TSysDepartment, bool>> predicate) { using (UCDbContext db=new UCDbContext()) { var models= db.TSysDepartments.Where(predicate); var sqlText = models.GetProperty("SqlText"); LogHelper.Debug(sqlText.ToString()); return models.Count(); } }