您可以很容易地创建一些不会包裹阅读器的辅助 OrmLite 扩展(适用于 v 3.9.55.0)。这很容易,因为您需要的方法是公开的。这是我的做法。
public static class MultiResultReaderOrmLiteExtensions
{
public static IList CustomConvertToList<T>(this IDataReader dataReader)
{
var modelDef = ModelDefinition<T>.Definition;
var type = typeof (T);
var fieldDefs = modelDef.AllFieldDefinitionsArray;
var listInstance = typeof(List<>).MakeGenericType(type).CreateInstance();
var to = (IList)listInstance;
var indexCache = dataReader.GetIndexFieldsCache(modelDef);
while (dataReader.Read())
{
var row = type.CreateInstance();
row.PopulateWithSqlReader(dataReader, fieldDefs, indexCache);
to.Add(row);
}
return to;
}
public static Dictionary<string, int> GetIndexFieldsCache(this IDataReader reader,
ModelDefinition modelDefinition = null)
{
var cache = new Dictionary<string, int>();
if (modelDefinition != null)
{
foreach (var field in modelDefinition.IgnoredFieldDefinitions)
{
cache[field.FieldName] = -1;
}
}
for (var i = 0; i < reader.FieldCount; i++)
{
cache[reader.GetName(i)] = i;
}
return cache;
}
}
然后你可以像这样调用:
using (var db = _connectionFactory.OpenDbConnection())
{
var cmd = db.api_GetSprocWithMultResults(id);
using (IDataReader reader = cmd.DbCommand.ExecuteReader())
{
meta = reader.CustomConvertToList<Element_Media_Meta>().Cast<Element_Media_Meta>().ToList();
reader.NextResult();
queues = reader.CustomConvertToList<Element_Media_ProcessQueue>().Cast<Element_Media_ProcessQueue>().ToList();
}
}