Search code examples
.net.net-coreormdapper

how add list, string and int into dapper select query


At the input of the method, I have a model that consists of { "Id": [ "string" ], "Status": "string", "systemStatus": "string" } I checked the request in ssms and it works, but in debugging this method I see different result. dapper takes the first id and return 1 users, but in the sql studio returns 6 people

public async Task<IEnumerable<User>> FindUser(Users model)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("@Id", model.Id);
            parameters.Add("@Status", model.Status);
            parameters.Add("@systemStatus", model.SystemStatus);

            IEnumerable<User> items = null;

            var sql = @"SELECT statusesid FROM userstatuses WHERE [id] IN (@Id) 
                                          AND [status] in (@Status) and [systemStatus] in (@systemStatus)";
            try
            {
                using (var db = new SqlConnection(connectionString))
                {
                    if (db.State != ConnectionState.Open)
                        await db.OpenAsync();

                    items = await db.QueryAsync<User>(sql, parameters);
                }
            }
            catch (Exception e)
            {
                _logger.Error(e);
            }
            return items;
        }

i try use string.Join(",", model.Id) but same result( how correctly add list, string and int into dapper select query


Solution

  • if you have list in sql you need write only "SELECT * FROM TableA WHERE id IN @ids" without brackets and dapper will put them down independently