简易orm 主要是为了旧平台查询方便
直接新建个文件即可
ExLogic.cs
public class ExLogic { public static int Execute(string sqlCommand, string dbConnection = "WebDb") { Database db = DatabaseFactory.CreateDatabase(dbConnection); DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand); try { return Convert.ToInt32(db.ExecuteScalar(dbCommand)); } catch (Exception ex) { Logging.WriteLog(ex); throw ex; } } /// <summary> /// 获取对象 /// </summary> /// <typeparam name="T">对象</typeparam> /// <param name="where">非必填</param> /// <returns></returns> public static T Get<T>(Expression<Func<T, bool>> where = null, string dbConnection = "WebDb") where T : class, new() { var whereSql = LambdaToSqlHelper.GetWhereSql(where); Database db = DatabaseFactory.CreateDatabase(dbConnection); string sqlCommand = $"SELECT * FROM {typeof(T).Name} WHERE " + (where == null ? "1==1" : whereSql); DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand); try { var res = new T(); using (IDataReader dr = db.ExecuteReader(dbCommand)) { if (dr.Read()) { var typeoft = typeof(T); var proper = typeoft.GetProperties(); foreach (var item in proper) { if (item.PropertyType == typeof(int)) item.SetValue(res, DataReaderHelper.GetInt32(dr, item.Name), null); else if (item.PropertyType == typeof(long)) item.SetValue(res, DataReaderHelper.GetInt64(dr, item.Name), null); else if (item.PropertyType == typeof(string)) item.SetValue(res, DataReaderHelper.GetString(dr, item.Name), null); else if (item.PropertyType == typeof(bool)) item.SetValue(res, DataReaderHelper.GetBoolean(dr, item.Name), null); else if (item.PropertyType == typeof(decimal)) item.SetValue(res, DataReaderHelper.GetDecimal(dr, item.Name), null); else if (item.PropertyType == typeof(double)) item.SetValue(res, DataReaderHelper.GetDouble(dr, item.Name), null); else if (item.PropertyType == typeof(DateTime)) item.SetValue(res, DataReaderHelper.GetDateTime(dr, item.Name), null); } } } return res; } catch (Exception ex) { throw; } } } /// <summary> /// 这部分代码网上可找,自行百度 ,我有稍作修改 /// </summary> public static class LambdaToSqlHelper { #region 基础方法 #region 获取条件语句方法 public static string GetWhereSql<T>(Expression<Func<T, bool>> func) where T : class { string res; if (func.Body is BinaryExpression) { //起始参数 BinaryExpression be = ((BinaryExpression)func.Body); res = BinarExpressionProvider(be.Left, be.Right, be.NodeType); } else if (func.Body is MethodCallExpression) { MethodCallExpression be = ((MethodCallExpression)func.Body); res = ExpressionRouter(func.Body); } else { res = " "; } return res; } #endregion 获取条件语句方法 #region 获取排序语句 order by public static string GetOrderSql<T>(Expression<Func<T, object>> exp) where T : class { var res = ""; if (exp.Body is UnaryExpression) { UnaryExpression ue = ((UnaryExpression)exp.Body); res = "order by `" + ExpressionRouter(ue.Operand).ToLower() + "`"; } else { MemberExpression order = ((MemberExpression)exp.Body); res = "order by `" + order.Member.Name.ToLower() + "`"; } return res; } #endregion 获取排序语句 order by #endregion 基础方法 #region 底层 public static bool In<T>(this T obj, T[] array) { return true; } public static bool NotIn<T>(this T obj, T[] array) { return true; } public static bool Like(this string str, string likeStr) { return true; } public static bool NotLike(this string str, string likeStr) { return true; } private static string GetValueStringByType(object oj) { if (oj == null) { return "null"; } else if (oj is ValueType) { return oj.ToString(); } else if (oj is string || oj is DateTime || oj is char) { return string.Format("'{0}'", oj.ToString()); } else { return string.Format("'{0}'", oj.ToString()); } } private static string BinarExpressionProvider(Expression left, Expression right, ExpressionType type) { var sb = string.Empty; //先处理左边 string reLeftStr = ExpressionRouter(left); sb += reLeftStr; sb += ExpressionTypeCast(type); //再处理右边 string tmpStr = ExpressionRouter(right); if (tmpStr == "null") { if (sb.EndsWith(" =")) { sb = sb.Substring(0, sb.Length - 2) + " is null"; } else if (sb.EndsWith("<>")) { sb = sb.Substring(0, sb.Length - 2) + " is not null"; } } else { //添加参数 sb += tmpStr; } return sb; } private static string ExpressionRouter(Expression exp) { string sb = string.Empty; if (exp is BinaryExpression) { BinaryExpression be = ((BinaryExpression)exp); return BinarExpressionProvider(be.Left, be.Right, be.NodeType); } else if (exp is MemberExpression) { MemberExpression me = ((MemberExpression)exp); if (!exp.ToString().StartsWith("value")) { return me.Member.Name; } else { var result = Expression.Lambda(exp).Compile().DynamicInvoke(); if (result == null) { return "null"; } else { return result.ToString(); } } } else if (exp is NewArrayExpression) { NewArrayExpression ae = ((NewArrayExpression)exp); StringBuilder tmpstr = new StringBuilder(); foreach (Expression ex in ae.Expressions) { tmpstr.Append(ExpressionRouter(ex)); tmpstr.Append(","); } //添加参数 return tmpstr.ToString(0, tmpstr.Length - 1); } else if (exp is MethodCallExpression) { MethodCallExpression mce = (MethodCallExpression)exp; string par = ExpressionRouter(mce.Arguments[0]); if (mce.Method.Name == "Like") { //添加参数用 return string.Format("({0} like {1})", par, ExpressionRouter(mce.Arguments[1])); } else if (mce.Method.Name == "NotLike") { //添加参数用 return string.Format("({0} Not like {1})", par, ExpressionRouter(mce.Arguments[1])); } else if (mce.Method.Name == "In") { //添加参数用 return string.Format("{0} In ({1})", par, ExpressionRouter(mce.Arguments[1])); } else if (mce.Method.Name == "NotIn") { //添加参数用 return string.Format("{0} Not In ({1})", par, ExpressionRouter(mce.Arguments[1])); } } else if (exp is ConstantExpression) { ConstantExpression ce = ((ConstantExpression)exp); if (ce.Value == null) { return "null"; } else { return $"'{ce.Value.ToString()}'"; } //对数值进行参数附加 } else if (exp is UnaryExpression) { UnaryExpression ue = ((UnaryExpression)exp); return ExpressionRouter(ue.Operand); } return null; } private static string ExpressionTypeCast(ExpressionType type) { switch (type) { case ExpressionType.And: case ExpressionType.AndAlso: return " AND "; case ExpressionType.Equal: return " ="; case ExpressionType.GreaterThan: return " >"; case ExpressionType.GreaterThanOrEqual: return ">="; case ExpressionType.LessThan: return "<"; case ExpressionType.LessThanOrEqual: return "<="; case ExpressionType.NotEqual: return "<>"; case ExpressionType.Or: case ExpressionType.OrElse: return " Or "; case ExpressionType.Add: case ExpressionType.AddChecked: return "+"; case ExpressionType.Subtract: case ExpressionType.SubtractChecked: return "-"; case ExpressionType.Divide: return "/"; case ExpressionType.Multiply: case ExpressionType.MultiplyChecked: return "*"; default: return null; } } #endregion 底层 }
使用
var entity=ExLogic.Get<DeviceNbIotMapping>(s => s.id == id);//返回单个
//需要返回列表等功能自行扩展
咩咩咩