@@ -291,6 +291,104 @@ public static List<TItem> ToList<TItem>(this IEnumerable<TItem> source, int capa
291
291
return result ;
292
292
}
293
293
294
+ /// <summary>
295
+ /// Creates a <see cref="Dictionary{TKey, TValue}"/> with pre-defined capacity from an <see cref="IEnumerable{T}"/>
296
+ /// according to a specified key selector function.
297
+ /// </summary>
298
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
299
+ /// <typeparam name="TKey">The type of the key elements.</typeparam>
300
+ /// <param name="source">A sequence to create a <see cref="Dictionary{TKey, TValue}"/> from.</param>
301
+ /// <param name="keySelector">A function to extract a key from each element.</param>
302
+ /// <param name="capacity">Initial dictionary capacity.</param>
303
+ /// <returns>A <see cref="Dictionary{TKey, TValue}"/> that contains keys and values.</returns>
304
+ public static Dictionary < TKey , TSource > ToDictionary < TSource , TKey > ( this IEnumerable < TSource > source , Func < TSource , TKey > keySelector , int capacity )
305
+ {
306
+ return ToDictionary < TSource , TKey , TSource > ( source , keySelector , InstanceSelector , capacity ) ;
307
+
308
+ static TSource InstanceSelector ( TSource x )
309
+ {
310
+ return x ;
311
+ }
312
+ }
313
+
314
+ /// <summary>
315
+ /// Creates a <see cref="Dictionary{TKey, TValue}"/> with pre-defined capacity from an <see cref="IEnumerable{T}"/>
316
+ /// according to a specified key selector function.
317
+ /// </summary>
318
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
319
+ /// <typeparam name="TKey">The type of the key elements.</typeparam>
320
+ /// <param name="source">A sequence to create a <see cref="Dictionary{TKey, TValue}"/> from.</param>
321
+ /// <param name="keySelector">A function to extract a key from each element.</param>
322
+ /// <param name="comparer">An <see cref="IEqualityComparer{T}"/> to compare keys.</param>
323
+ /// <param name="capacity">Initial dictionary capacity.</param>
324
+ /// <returns>A <see cref="Dictionary{TKey, TValue}"/> that contains keys and values.</returns>
325
+ public static Dictionary < TKey , TSource > ToDictionary < TSource , TKey > (
326
+ this IEnumerable < TSource > source ,
327
+ Func < TSource , TKey > keySelector ,
328
+ IEqualityComparer < TKey > comparer ,
329
+ int capacity )
330
+ {
331
+ return ToDictionary < TSource , TKey , TSource > ( source , keySelector , InstanceSelector , comparer , capacity ) ;
332
+
333
+ static TSource InstanceSelector ( TSource x )
334
+ {
335
+ return x ;
336
+ }
337
+ }
338
+
339
+ /// <summary>
340
+ /// Creates a <see cref="Dictionary{TKey, TValue}"/> with pre-defined capacity from an <see cref="IEnumerable{T}"/>
341
+ /// according to a specified key selector function.
342
+ /// </summary>
343
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
344
+ /// <typeparam name="TKey">The type of the key elements.</typeparam>
345
+ /// <param name="source">A sequence to create a <see cref="Dictionary{TKey, TValue}"/> from.</param>
346
+ /// <param name="keySelector">A function to extract a key from each element.</param>
347
+ /// <param name="elementSelector">A funtion to extract a value from each element.</param>
348
+ /// <param name="capacity">Initial dictionary capacity.</param>
349
+ /// <returns>A <see cref="Dictionary{TKey, TValue}"/> that contains keys and values.</returns>
350
+ public static Dictionary < TKey , TValue > ToDictionary < TSource , TKey , TValue > (
351
+ this IEnumerable < TSource > source ,
352
+ Func < TSource , TKey > keySelector ,
353
+ Func < TSource , TValue > elementSelector ,
354
+ int capacity )
355
+ {
356
+ return ToDictionary ( source , keySelector , elementSelector , null , capacity ) ;
357
+ }
358
+
359
+ /// <summary>
360
+ /// Creates a <see cref="Dictionary{TKey, TValue}"/> with pre-defined capacity from an <see cref="IEnumerable{T}"/>
361
+ /// according to a specified key selector function.
362
+ /// </summary>
363
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
364
+ /// <typeparam name="TKey">The type of the key elements.</typeparam>
365
+ /// <param name="source">A sequence to create a <see cref="Dictionary{TKey, TValue}"/> from.</param>
366
+ /// <param name="keySelector">A function to extract a key from each element.</param>
367
+ /// <param name="elementSelector">A funtion to extract a value from each element.</param>
368
+ /// <param name="comparer">An <see cref="IEqualityComparer{T}"/> to compare keys.</param>
369
+ /// <param name="capacity">Initial dictionary capacity.</param>
370
+ /// <returns>A <see cref="Dictionary{TKey, TValue}"/> that contains keys and values.</returns>
371
+ public static Dictionary < TKey , TValue > ToDictionary < TSource , TKey , TValue > (
372
+ this IEnumerable < TSource > source ,
373
+ Func < TSource , TKey > keySelector ,
374
+ Func < TSource , TValue > elementSelector ,
375
+ IEqualityComparer < TKey > equalityComparer ,
376
+ int capacity )
377
+ {
378
+ ArgumentValidator . EnsureArgumentNotNull ( source , nameof ( source ) ) ;
379
+ ArgumentValidator . EnsureArgumentNotNull ( keySelector , nameof ( keySelector ) ) ;
380
+ ArgumentValidator . EnsureArgumentNotNull ( elementSelector , nameof ( elementSelector ) ) ;
381
+ ArgumentValidator . EnsureArgumentIsGreaterThanOrEqual ( capacity , 0 , nameof ( capacity ) ) ;
382
+
383
+ var dictionary = equalityComparer != null
384
+ ? new Dictionary < TKey , TValue > ( capacity , equalityComparer )
385
+ : new Dictionary < TKey , TValue > ( capacity ) ;
386
+ foreach ( var item in source ) {
387
+ dictionary . Add ( keySelector ( item ) , elementSelector ( item ) ) ;
388
+ }
389
+ return dictionary ;
390
+ }
391
+
294
392
/// <summary>
295
393
/// Gets the items from the segment.
296
394
/// </summary>
0 commit comments