年少的隔壁老王 2018-07-24 08:50 采纳率: 60%
浏览 1966
已采纳

C# api接口不固定参数get传参

要写一个C#的api访问接口,get方式传参,需求:参数个数和参数名不固定。例如:
参数是 element,time,class,则后台实现 select element from tab1 where datatime>time and className=class;
如果传参数 element, time,class,order,则后台实现 select element from tab1 where datatime>time and className=class order by order;
还要类似很多这种情况, 怎么写一个通用的方法实现

  • 写回答

6条回答 默认 最新

  • nihaimeiyoudenglu 2018-07-24 09:16
    关注

    你这个是针对sql脚本的,首先,你要针对这种形式写一个接收参数的class.
    例:
    public class searchClass{
    public string display{get;set;}//多个参数可以用,形式,例 如:a,b,c,element
    public string table{get;set;}
    public List conditions{get;set;}
    //可以加更多参
    }

    public class searchCondition{
    public string key {get;set;}
    public string value{get;set;}
    public string fn{get;set;}// > < = ...
    }

    public IctionResult Api(searchClass param)
        {
          string sql = " select "+param.display+" from " + param.param ;
          if(param.conditions!=null && param.conditions.count()>0)
          sql+=" where 1=1 ";
    

    foreach (var item in param.conditions) sql += " and " + item.key + item.fn + item.value;
    if (o != null) sql += " order by" + o;
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?