简易orm 主要是为了旧平台查询方便
直接新建个文件即可
1 | ExLogic.cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | 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 底层 } |
使用
1 | var entity=ExLogic.Get<DeviceNbIotMapping>(s => s.id == id); //返回单个<br>//需要返回列表等功能自行扩展<br> |
咩咩咩
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构