/// <summary> /// 根据条件去进行查询 /// ConditionBuilderVisitor2 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="expression">条件表达式</param> /// <returns>单个实体</returns> //public T GetModel<T>(Expression<Func<T, bool>> expression) //{ // ConditionBuilderVisitor2 visitor = new ConditionBuilderVisitor2(expression); // Tuple<string, List<MySqlParameter>> res = visitor.Condition(); // string sql = SqlBuider<User>.GetSql(dataType.QCondition) + res.Item1; // return Excet<T>(sql, res.Item2.ToArray(), cmd => // { // T t = Activator.CreateInstance<T>(); // var reader = cmd.ExecuteReader(); // if (reader.Read()) // { // foreach (System.Reflection.PropertyInfo item in t.GetType().GetPropertiesName(false)) // item.SetValue(t, reader[item.Name] is DBNull ? null : reader[item.Name]); // } // return t; // }); // return default(T); //} /// <summary> /// 自写表达式树 根据条件去查询实体 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="expression">表达式</param> /// <returns>单个实体</returns> public T GetModel <T>(Expression <Func <T, bool> > expression) { QinVisitor <T> visitor = new QinVisitor <T>(expression); (string res, Dictionary <string, object> pairs) = visitor.GetResult(); List <MySqlParameter> pp = pairs.GetMySqlPar(); string sql = SqlBuider <T> .GetSql(dataType.QCondition) + res; return(Excet <T>(sql, pp.ToArray(), cmd => { T t = Activator.CreateInstance <T>(); var reader = cmd.ExecuteReader(); if (reader.Read()) { foreach (System.Reflection.PropertyInfo item in t.GetType().GetPropertiesName(false)) { item.SetValue(t, reader[item.Name] is DBNull ? null : reader[item.Name]); } } else { return default; } return t; })); }
/// <summary> /// 根据条件去进行查询 /// ConditionBuilderVisitor2 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="expression">条件表达式</param> /// <returns></returns> public List <T> GetList <T>(Expression <Func <T, bool> > expression) { // ConditionBuilderVisitor2 zlc = new ConditionBuilderVisitor2(expression); // var res = zlc.Condition(); QinVisitor <T> visitor = new QinVisitor <T>(expression); (string res, Dictionary <string, object> pairs) = visitor.GetResult(); List <MySqlParameter> pp = pairs.GetMySqlPar(); string sql = SqlBuider <T> .GetSql(dataType.QCondition) + res; return(Excet(sql, pp.ToArray(), cmd => { T t = Activator.CreateInstance <T>(); List <T> list = new List <T>(); var read = cmd.ExecuteReader(); while (read.Read()) { foreach (System.Reflection.PropertyInfo prop in t.GetType().GetPropertiesName(false)) { prop.SetValue(t, read[prop.Name] is DBNull ? null : read[prop.Name]); } list.Add(t); t = Activator.CreateInstance <T>();//重置新的对象 } return list; })); }
/// <summary> /// 二次解析 /// </summary> /// <param name="node"></param> /// <returns></returns> private string GetSublevel(Expression node) { QinVisitor <TS> z_ = new QinVisitor <TS>(node); return(z_.GetResult().Item1.ToString()); }