我确实使用下面的代码扩展了 GridReader;
/// <summary>
///
/// </summary>
public static class Extentions
{
/// <summary>
/// Maps the specified reader.
/// </summary>
/// <typeparam name="TFirst">The type of the first.</typeparam>
/// <typeparam name="TSecond">The type of the second.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="reader">The reader.</param>
/// <param name="firstKey">The first key.</param>
/// <param name="secondKey">The second key.</param>
/// <param name="addChildren">The add children.</param>
/// <returns></returns>
public static IEnumerable<TFirst> Map<TFirst, TSecond, TKey>
(
this Dapper.SqlMapper.GridReader reader,
Func<TFirst, TKey> firstKey,
Func<TSecond, TKey> secondKey,
Action<TFirst, IEnumerable<TSecond>> addChildren
)
{
var first = reader.Read<TFirst>().ToList();
var childMap = reader
.Read<TSecond>()
.GroupBy(s => secondKey(s))
.ToDictionary(g => g.Key, g => g.AsEnumerable());
foreach (var item in first)
{
IEnumerable<TSecond> children;
if (childMap.TryGetValue(firstKey(item), out children))
{
addChildren(item, children);
}
}
return first;
}
}
阅读器已被处置;这可能在所有数据都被消耗后发生
此处的品脱然后当调用该方法时,阅读器会在第一次读取所有数据,但会输入该方法
var first = reader.Read<TFirst>().ToList();
这一行给出了上面的例外。除了查询之外,还有什么方法可以让小巧的阅读器保持活力。
提前致谢 。
笔记; 该方法是这样调用的
var sql = (@"SELECT *
FROM [pubs].[dbo].[authors] as a
right join pubs.dbo.titleauthor as b
ON a.au_id = b.au_idT");
var data = connection.QueryMultiple(sql).Map<authors, titleauthor, string>(
au => au.au_id,
tit => tit.au_idT,
(au, tits) => { au.titleauthor = tits; });