Dapper不断给我以下错误
System.InvalidOperationException:“在此上下文中不允许使用可枚举的参数序列(数组,列表等)”
代码如下:
var ids = result.Result.Select(x => new { x.Id } );
query = @"
SELECT [A], [B], [C]
FROM [MyTable]
WHERE [C] IN @Ids
";
var resultTwo = Connection.Query<MyObject>(query, ids)?.ToList();
我也尝试过:
var ids = result.Result.Select(x => x.Id );
答案 0 :(得分:1)
借助DynamicParameters,我可以运行像您这样的查询
var ids = Enumerable.Range(1, 100).ToList();
var parameters = new DynamicParameters();
parameters.Add("@ids", ids);
var recs = cnn.Query<City>("SELECT * FROM Cities Where IDCity in @ids", parameters);
这使我认为(如上面的评论中已经解释的那样)问题出在缺少的新{ids} 上,所以可能只是
var resultTwo = Connection.Query<MyObject>(query, new { ids} )?.ToList();