Is it possible to do this build this query with Dapper?

对着背影说爱祢 提交于 2019-12-11 13:59:22

问题


Is it possible do do the following? I tried, but when it got to the Query, it just said there was a null reference.

   var builder = new StringBuilder("select * from my table1 where 1 = 1");

if(x==1)
    builder.Append(" and x = @x");

if(y==2)
    builder.Append(" and y = @y");

// when it gets here, it just says null reference

 db.Query<table1>(builder.ToString(), new {x,y});

I got SqlBuilder to run in .net 3.5, but when I do this:

var builder = new SqlBuilder();

var sql = builder.AddTemplate("select * from table /**where**/ /**orderby**/");

 builder.Where("a = @a", new { a = 1 })
        .OrWhere("b = @b", new { b = 2 });

I expected select * from table WHERE a = @a OR ( b = @b )

but I got:

I expected select * from table WHERE a = @a AND ( b = @b )


回答1:


Presumably this was due to this bug. The attempted fix was made on Jul 31 2016 however there are still issues with this approach. The plan is that this would be fixed in the next major release.



来源:https://stackoverflow.com/questions/29156948/is-it-possible-to-do-this-build-this-query-with-dapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!