最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C# & Dapper : Where In clause using dynamic parameters - Stack Overflow

programmeradmin4浏览0评论

I have a simple query with multiple parameters and one of them is an IN parameter, ie:

select d.TitleName 
from dbo.Device d 
where d.Id in @ids 
  and d.Disabled = @isDisabled

I need to build these parameters dynamically so I cannot do this:

var model = await conn.QueryAsync<DeviceMaintenanceTableModel>(myQuery, new { ids, isDisabled });

I tried passing in a dynamic parameter object from an IDictionary<string, object> class and got an error:

var ids = new[] { 4456, 22686 };
var p = new Dictionary<string, object>();
p.Add("@ids", ids);

var dp = new DynamicParameters(p);
var model = await conn.QueryAsync<DeviceMaintenanceTableModel>(myQuery, dp);

Error:

No mapping exists from object type Newtonsoft.Json.Linq.JValue to a known managed provider native type.

I ended up just doing string.Join and passing it in as a string parameter type, but I was wondering if there is a way to use Dapper's WhereIn clause parameter with the DynamicParameters class?

发布评论

评论列表(0)

  1. 暂无评论