我有以下代码C#:
IMapper mapper = new Mapper(session);
//...
var names = new [] {"n1", "n2", "n3"};
//Generation of question marks
var inStr = string.Join(",", names.Select(n=>"?"));
var users = mapper.Fetch<User>($"SELECT * FROM users WHERE name IN ({inStr})", names);
这种方法有效。 但也许有人知道一个更优雅的解决方案?
答案 0 :(得分:2)
要在单个参数中提供动态值列表,请使用IN运算符,后跟查询中没有括号的问号占位符。
例如:
var names = new [] {"n1", "n2", "n3"};
var users = mapper.Fetch<User>("SELECT * FROM users WHERE name IN ?", names);