QueryableExtensions.ToDictionaryAsync<TSource, TKey, TElement> 方法 (IQueryable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, CancellationToken)
[本页针对的是实体框架版本 6。最新版本以“实体框架”NuGet 包的形式提供。有关实体框架的更多信息,请参见 msdn.com/data/ef。]
从 IQueryable<T> 创建 Dictionary<TKey, TValue>,方法是:根据指定的键选择器函数和元素选择器函数来异步枚举它。
命名空间: System.Data.Entity
程序集: EntityFramework(在 EntityFramework.dll 中)
语法
声明
<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")> _
<ExtensionAttribute> _
Public Shared Function ToDictionaryAsync(Of TSource, TKey, TElement) ( _
source As IQueryable(Of TSource), _
keySelector As Func(Of TSource, TKey), _
elementSelector As Func(Of TSource, TElement), _
cancellationToken As CancellationToken _
) As Task(Of Dictionary(Of TKey, TElement))
用法
Dim source As IQueryable(Of TSource)
Dim keySelector As Func(Of TSource, TKey)
Dim elementSelector As Func(Of TSource, TElement)
Dim cancellationToken As CancellationToken
Dim returnValue As Task(Of Dictionary(Of TKey, TElement))
returnValue = source.ToDictionaryAsync(keySelector, _
elementSelector, cancellationToken)
[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(
this IQueryable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector,
CancellationToken cancellationToken
)
[SuppressMessageAttribute(L"Microsoft.Design", L"CA1006:DoNotNestGenericTypesInMemberSignatures")]
[ExtensionAttribute]
public:
generic<typename TSource, typename TKey, typename TElement>
static Task<Dictionary<TKey, TElement>^>^ ToDictionaryAsync(
IQueryable<TSource>^ source,
Func<TSource, TKey>^ keySelector,
Func<TSource, TElement>^ elementSelector,
CancellationToken cancellationToken
)
[<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")>]
static member ToDictionaryAsync :
source:IQueryable<'TSource> *
keySelector:Func<'TSource, 'TKey> *
elementSelector:Func<'TSource, 'TElement> *
cancellationToken:CancellationToken -> Task<Dictionary<'TKey, 'TElement>>
JScript does not support generic types and methods.
类型参数
- TSource
source 的元素的类型。
- TKey
keySelector 返回的键类型。
- TElement
elementSelector 返回的值的类型。
参数
- source
类型:System.Linq.IQueryable<TSource>
一个将从中创建 Dictionary<TKey, TValue> 的 IQueryable<T>。
- keySelector
类型:System.Func<TSource, TKey>
用于从每个元素中提取键的函数。
- elementSelector
类型:System.Func<TSource, TElement>
用于从每个元素产生结果元素值的转换函数。
- cancellationToken
类型:System.Threading.CancellationToken
等待任务完成期间要观察的 CancellationToken。
返回值
类型:System.Threading.Tasks.Task<Dictionary<TKey, TElement>>
表示异步操作的任务。任务结果包含一个 Dictionary<TKey, TValue>,后者包含从输入序列选择的类型 TElement 的值。
使用说明
在 Visual Basic 和 C# 中,可以在 IQueryable<TSource> 类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关详细信息,请参阅https://msdn.microsoft.com/zh-cn/library/bb384936(v=vs.113)或https://msdn.microsoft.com/zh-cn/library/bb383977(v=vs.113)。
备注
不支持对同一上下文实例执行多个活动操作。 使用“await”以确保在对此上下文调用另一个方法前完成了所有异步操作。