In the current build, the answer to this is no for two reasons:
- the code tries to filter out unused parameters - and currently deletes all of them because it cannot find anything like
@id :id or ?id in sql - the code for adding values ββfrom types uses an arbitrary (well, ok: alphabetical) order for parameters (since reflection gives no guarantees regarding the order of members), making positional anonymous arguments unstable
The good news is that both are fixed
- we can make the filter condition conditional
- we can define a category of types that has a constructor that matches all property names, and use the position of the constructor argument to determine the synthetic order of properties - anonymous types fall into this category
Performing these changes in my local clone, the following now executes:
// see https://stackoverflow.com/q/18847510/23354 public void TestOleDbParameters() { using (var conn = new System.Data.OleDb.OleDbConnection( Program.OleDbConnectionString)) { var row = conn.Query("select Id = ?, Age = ?", new DynamicParameters( new { foo = 12, bar = 23 } // these names DO NOT MATTER!!! ) { RemoveUnused = false } ).Single(); int age = row.Age; int id = row.Id; age.IsEqualTo(23); id.IsEqualTo(12); } }
Note that I am currently using DynamicParameters here to avoid adding even more overloads to Query / Query<T> - because this will need to be added to a large number of methods. Adding it to DynamicParameters resolves it in one place.
I am open to feedback before I put it forward - is this right for you?
Edit: with the addition of funky smellsLikeOleDb (no, not a joke), we can do it even more directly:
// see https://stackoverflow.com/q/18847510/23354 public void TestOleDbParameters() { using (var conn = new System.Data.OleDb.OleDbConnection( Program.OleDbConnectionString)) { var row = conn.Query("select Id = ?, Age = ?", new { foo = 12, bar = 23 } // these names DO NOT MATTER!!! ).Single(); int age = row.Age; int id = row.Id; age.IsEqualTo(23); id.IsEqualTo(12); } }
source share