SqlHelper简单实现(通过Expression和反射)10.使用方式
以下是整个SqlHelper的Demo:
1 public Result<List<ArticleDTO>> GetIndexArticleList(int count, int userID) 2 { 3 var exp = DbUtilityFactory.GetDbUtility().GetSqlExpression<ArticleEntity>(); 4 exp.Where(a => a.userID == userID); 5 exp.OrderByDescending(a=>a.articleDate); 6 var data = DbUtilityFactory.GetDbUtility().Paged<ArticleEntity>(a=>a.articleDate,1,6); 7 if(data.Any()) 8 { 9 return new Result<List<ArticleDTO>>() 10 { 11 IsSuccess = true, 12 ReturnMessage = "成功", 13 ReturnValue = data.Select<ArticleEntity, ArticleDTO>(a => new ArticleDTO() 14 { 15 articleAbstract = a.articleAbstract, 16 articleDate = a.articleDate, 17 articleID = a.articleID, 18 articleTitle = a.articleTitle 19 }).ToList() 20 }; 21 }else 22 { 23 return new Result<List<ArticleDTO>>() 24 { 25 IsSuccess = false, 26 ReturnMessage = "没有记录" 27 }; 28 } 29 }
比如在运行到第5行exp.OrderByDescending(a=>a.articleDate)时:
这里就是实现了Lambda到sql的转换。
完整Demo地址:https://github.com/Racpheal/Construction.git