Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a List<Dictionary<string,string>> with ServiceStack ORMLite SqlList
    text
    copied!<p>Has anyone else seen problems in trying to use a generic List of Dictionary&lt; string,string > (or string,object) with the <strong>SqlList</strong> feature of ORMLite?</p> <pre><code>var data = Db.SqlList&lt;Dictionary&lt;string, string&gt;&gt;("exec my_storedproc p1,p2"); </code></pre> <p>It throws the following exception:</p> <pre><code>Incorrect number of arguments supplied for call to method 'System.String get_Item(System.String)' </code></pre> <p>I haven't debugged into the guts of it yet, but it appears to crash here when extracting the field defs:</p> <pre><code>public static T ConvertTo&lt;T&gt;(this IDataReader dataReader) { var fieldDefs = ModelDefinition&lt;T&gt;.Definition.AllFieldDefinitionsArray; </code></pre> <p>note: the underlying dialect provider is for sqlserver</p> <p><strong>Update #1</strong> digging deeper, the crash is triggered here, on the "String Item" property of the Dictionary:</p> <pre><code>namespace ServiceStack.OrmLite public static class PropertyInvoker public static PropertyGetterDelegate GetPropertyGetterFn(this PropertyInfo propertyInfo) { ... var exprCallPropertyGetFn = Expression.Call(instanceParam, getMethodInfo); </code></pre> <p><strong>Update #2</strong> compiling the OrmLite with No_Expressions returns the propertygetter a little differently and lets it flow through, and the call to SqlList returns the correct number of items; however, each Dictionary has 0 items. Seems it was able to create a new Dictionary for each record, but not able to insert the fields/columns.</p> <p>Thoughts?</p> <p><strong>Update #3</strong> (hacked solution)</p> <p>For those interested, I manually added a fix to do what I needed. In OrmLiteReadExtensions, I added an "IsDictionary" to go with the IsScalar check, and then added a ConvertToDictionary. Not the cleanest solution I'm sure, but works. Here are the snippets:</p> <pre><code>public static bool IsDictionary&lt;T&gt;() { return typeof(IDictionary).IsAssignableFrom(typeof(T)); } internal static List&lt;T&gt; SqlList&lt;T&gt;(this IDbCommand dbCmd, string sql, object anonType = null) { if (anonType != null) dbCmd.SetParameters(anonType, true); dbCmd.CommandText = sql; using (var dbReader = dbCmd.ExecuteReader()) return IsScalar&lt;T&gt;() ? dbReader.GetFirstColumn&lt;T&gt;() : IsDictionary&lt;T&gt;() ? dbReader.ConvertToDictionary&lt;T&gt;() : dbReader.ConvertToList&lt;T&gt;(); } public static List&lt;T&gt; ConvertToDictionary&lt;T&gt;(this IDataReader dataReader) [...] while (dataReader.Read()) { var row = CreateInstance&lt;T&gt;(); var roww = (row as Dictionary&lt;string, object&gt;); for (int f = 0; f &lt; fields; f++) { roww.Add(dataReader.GetName(f), dataReader.GetValue(f)); } to.Add(row); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload