【发布时间】:2020-12-16 15:07:11
【问题描述】:
我有一个问题,我的方法中有相同的 Func。我是单独拿出来的,但现在的问题是如何给它传参数。我想在几个地方使用它,但是我不知道如何从QueryAsync方法中获取必要的参数
我的 func 委托
private Func<Authors, AuthorInPrintingEditions, PrintingEditions, Authors> _relationDelegate =
(Authors authors, AuthorInPrintingEditions relation, PrintingEditions printingEdition) =>
{
if (!_dictionaryResult.ContainsKey(authors.Id))
{
_dictionaryResult.Add(authors.Id, new Authors()
{
Id = authors.Id,
Name = authors.Name,
AuthorInPrintingEditions = printingEdition == null
? new List<AuthorInPrintingEditions>()
: new List<AuthorInPrintingEditions>()
{
new AuthorInPrintingEditions()
{
PrintingEdition = printingEdition
}
}
});
}
else
{
if (printingEdition != null)
{
_dictionaryResult[authors.Id].AuthorInPrintingEditions.Add(new AuthorInPrintingEditions()
{
PrintingEdition = printingEdition
});
}
}
return authors;
};
我的方法,当我想用的时候
using (var conneсtion = CreateConnection())
{
await conneсtion.QueryAsync<Authors, AuthorInPrintingEditions, PrintingEditions, Authors>(query,
(authors, authorInPrintingEditions, printingEdition), <--- in this row I want use delegate
new { skip = skip, pageSize = pageSize }, splitOn: "Id,AuthorId,Id");
return _dictionaryResult.Values.ToList();
}
不明白怎么给它传参数,一般情况下是否可行,还是复制粘贴会比较容易
【问题讨论】:
-
在那一行输入
_relationDelegate。 -
参数化可重用委托的一个选项是将委托和参数公开为某个类的实例成员。然后,当您需要此委托时,创建该类的实例,设置参数并将委托传递给异步查询。
-
全错了...返回
IEnumerable<AuthorPrintingEditionPair>然后使用ToDictionary(...)扩展 -
@Dusan 是的,我做到了,它成功了
标签: c# sql .net sql-server dapper