使用Dapper参数化查询(二)
查询封装
/// <summary> /// 分页查询条件 /// </summary> public class PageWhere { /// <summary> /// /// </summary> public QueryWhere queryWhere { get; set; } /// <summary> /// 每页显示数量 /// </summary> public int pageSize { get; set; } /// <summary> /// 第几页 /// </summary> public int pageIndex { get; set; } /// <summary> /// 排序字段 /// </summary> public string filedOrder { get; set; } } /// <summary> /// 查询条件 /// </summary> public class QueryWhere { public QueryWhere() { listWhere = new List<string>(); dicWhere = new List<DicWhere>(); } /// <summary> /// 查询条件 /// </summary> public List<string> listWhere { get; set; } /// <summary> /// 赋值 /// </summary> public List<DicWhere> dicWhere { get; set; } } /// <summary> /// 字段==值 /// </summary> public class DicWhere { /// <summary> /// 字段 /// </summary> public string filed { get; set; } /// <summary> /// 对应值 /// </summary> public object value { get; set; } }
转换为dapper的dy
QueryParameter qp = new QueryParameter(); qp.listWhere = pageWhere.queryWhere.listWhere; pageWhere.queryWhere.dicWhere.ForEach(item => { qp.dynamicParameter.Add(item.filed, item.value); }); return _prerecordBLL.GetList(qp, pageWhere.pageSize, pageWhere.pageIndex, pageWhere.filedOrder);
调用
if (!string.IsNullOrWhiteSpace(gltxtcinput.Text)) { qw.listWhere.Add(" a.cinput=@cinput "); qw.dicWhere.Add(new DicWhere() { filed = "@cinput", value = gltxtcinput.EditValue }); }