//根据值类型,动态组装参数名称或值 private string GetParams(string key, object value, ISelectBuilder selectBuilder = null) { if (value != null) { var list = new List <string>(); var type = value.GetType(); var array = value as Array; if (array != null) {//数组类型 for (int i = 0; i < array.Length; i++) { var pName = $"@{key}_{i}"; list.Add(pName); if (selectBuilder != null) {//设置参数到查询器中 var val = array.GetValue(i); selectBuilder.Parameters($"{key}_{i}", val); } }//end for //各参数之间使用 return(string.Join(",", list)); }//end if return(string.Empty); } throw new ArgumentNullException("使用了In或NotIn方式查询,但没有给出范围值"); }
//创建一个查询对象构造器 private ISelectBuilder CreateBuilder(DbContext db, ExecuteModel executeModel) { //获取当前类名 var tableName = GetTableName(executeModel.Entity.GetType()); //查询器 ISelectBuilder selectBuilder = db.Select(tableName); //获取条件sql var whereSql = GetConitionsql(executeModel.Conditions); //添加SQL条件 selectBuilder.Where(whereSql); executeModel.Conditions?.ForEach(p => { //先设置参数,组装SQL if (p.Oper == Operational.In || p.Oper == Operational.NotIn) { //In 或 not in 通过设置参数方式 GetParams(p.Key, p.Value, selectBuilder); } else { selectBuilder.Parameters(p.Key, p.Value); } }); if (executeModel.OrderColumn.IsNotNullOrEmpty()) {//如果包括了排序字段,则使用排序 selectBuilder.OrderBy(executeModel.OrderColumn); }//end if //返回组装的对象 return(selectBuilder); }