Search code examples
c#dapper

Dapper - enumerable sequence of parameters is not allowed


Dapper keeps giving me the following error

System.InvalidOperationException: 'An enumerable sequence of parameters (arrays, lists, etc) is not allowed in this context'

Here is the code:

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();

I have also tried:

    var ids = result.Result.Select(x => x.Id );

Solution

  • I am able to run a query like yours with the help of the 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);
    

    This make me think that (as already explained in a comment above) the problem is in the missing new {ids} so it could be simply

    var resultTwo = Connection.Query<MyObject>(query, new { ids} )?.ToList();