ExpressionToSQL产生无效的查询

时间:2019-03-10 17:32:04

标签: c# expression dapper func expressiontosql

我正在将Dapper与ExpressionToSQL nuget软件包一起使用。我有一个查询,如下所示:

Set = new Table{Schema = "Sch",Name = "tbl1"};
Columns = x => new { x.Id};
Conditions = x => x.Name== request.Name && x.Date == request.Date;

Where<TQueryIn, object> query = Sql.Select(Columns, Set).Where(Conditions);
...
connection.QueryAsync(query);

其中request.Name的值为“ T1”,而request.Date的值为“ 20121020”。

运行上述查询时,得到以下查询:

SELECT a.[Id] FROM [Sch].[tbl1] AS a WHERE a.[Name] = @Name AND a.[Date] = @Date

如您所见,request.Namerequest.Date的值被@Name@Date取代!

我需要看的是:

SELECT a.[Id] FROM [Sch].[tbl1] AS a WHERE a.[Name] = 'T1' AND a.[Date] = '20121020'

这绝对不是Dapper的问题,而是ExpressionToSQL包的问题。

有什么方法可以解决此问题,并将Expression<Func<T,bool>>转换为具有上述输出的字符串值吗?

1 个答案:

答案 0 :(得分:1)

我不熟悉Dapper,但是在查看ExpressionToSQL中的测试之后,似乎可以在QueryAsync的第二个参数上提供参数,因此,我认为这应该可行:

Set = new Table{Schema = "Sch",Name = "tbl1"};
Columns = x => new { x.Id};
Conditions = x => x.Name== request.Name && x.Date == request.Date;

Where<TQueryIn, object> query = Sql.Select(Columns, Set).Where(Conditions);
...
connection.QueryAsync(query, request);

也发现了这一点,可能有用: https://dapper-tutorial.net/parameter-anonymous