像Dapper这样的带有DynamicParameters的运算符

时间:2018-07-19 04:52:00

标签: c# entity-framework sql-server-2008 dynamic dapper

现在简化的问题。 我正在为Dapper动态生成sql文本查询,并且我有需要在其中生成带有Dapper参数的Like运算符查询的要求。我尝试使用

 public QueryStatement<T> 
 {
   public string Column;
   public object Value;
 }


 private void CreateEndsWithParameter(QueryStatement<TClass> statement, 
StringBuilder text, DynamicParameters parameters)
    {
        var random = Counter;

        var column = $"[{statement.Column}]";


        var paramName = statement.Column + random;

        **//1ST APPROACH : -----------IS IS NOT WORKING------------------**
        //parameters.Add(paramName, "%" + Convert.ToString(statement.Value));

       **// SECOND APPROACH : ----------NOT WORKING-----------------**
        dynamic parameter= new System.Dynamic.ExpandoObject();
        var dictionary = (IDictionary<string, object>)parameter;

        dictionary.Add(paramName, "%" + Convert.ToString(statement.Value));
        parameters.AddDynamicParams(person);

        **//THIRD APPROACH: WORKING BUT CAN'T BE USED DUE ANONYMOUS KEY IS GENERATED AT RUNTIME**
        //var par=  new {parameter="%" + Convert.ToString(statement.Value)};
        //parameters.AddDynamicParams(par);


        text.Append($" {column} LIKE @{paramName} ) ");
    }

我也尝试使用下面的代码,但是那也不起作用(在参数值中未添加“%”)

var condition= "( [column] LIKE '%'+ @column_val) And ([column1] = @column1_val ) ";
//CONCAT CANT BE USED DUE TO SQL SERVER 2008
var condition ="( [column] LIKE CONCAT('%', @column_val)) And ([column1] =  @column1_val )"

0 个答案:

没有答案