Enumerable.ToDictionary Metoda

Definicja

Tworzy obiekt na Dictionary<TKey,TValue> podstawie elementu IEnumerable<T>.

Przeciążenia

ToDictionary<TKey,TValue>(IEnumerable<ValueTuple<TKey,TValue>>, IEqualityComparer<TKey>)

Tworzy słownik na podstawie wyliczenia zgodnie z określonym modułem porównywania równości kluczy.

ToDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)

Tworzy słownik na podstawie wyliczenia zgodnie z określonym modułem porównującym klucz.

ToDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)

Tworzy słownik na podstawie wyliczenia zgodnie z domyślnym modułem porównującym dla typu klucza.

ToDictionary<TKey,TValue>(IEnumerable<ValueTuple<TKey,TValue>>)

Tworzy słownik na podstawie wyliczenia zgodnie z domyślnym modułem porównującym dla typu klucza.

ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)

Tworzy obiekt Dictionary<TKey,TValue>IEnumerable<T> na podstawie określonych funkcji selektora kluczy i selektora elementów.

ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)

Tworzy element Dictionary<TKey,TValue> na podstawie IEnumerable<T> określonej funkcji selektora kluczy, modułu porównującego i funkcji selektora elementów.

ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

Tworzy obiekt Dictionary<TKey,TValue> na podstawie IEnumerable<T> określonej funkcji selektora klucza.

ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)

Tworzy element Dictionary<TKey,TValue>IEnumerable<T> na podstawie określonej funkcji selektora kluczy i modułu porównania kluczy.

ToDictionary<TKey,TValue>(IEnumerable<ValueTuple<TKey,TValue>>, IEqualityComparer<TKey>)

Tworzy słownik na podstawie wyliczenia zgodnie z określonym modułem porównywania równości kluczy.

public:
generic <typename TKey, typename TValue>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::Dictionary<TKey, TValue> ^ ToDictionary(System::Collections::Generic::IEnumerable<ValueTuple<TKey, TValue>> ^ source, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.Dictionary<TKey,TValue> ToDictionary<TKey,TValue> (this System.Collections.Generic.IEnumerable<(TKey Key, TValue Value)> source, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member ToDictionary : seq<ValueTuple<'Key, 'Value>> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Value>
<Extension()>
Public Function ToDictionary(Of TKey, TValue) (source As IEnumerable(Of ValueTuple(Of TKey, TValue)), comparer As IEqualityComparer(Of TKey)) As Dictionary(Of TKey, TValue)

Parametry typu

TKey

Typ kluczy z elementów elementu source.

TValue

Typ wartości z elementów elementu source.

Parametry

source
IEnumerable<ValueTuple<TKey,TValue>>

Wyliczenie do utworzenia słownika.

comparer
IEqualityComparer<TKey>

Porównujący równość do porównywania kluczy.

Zwraca

Słownik zawierający klucze i wartości z sourceelementu .

Wyjątki

source jest odwołaniem null .

source zawiera co najmniej jeden zduplikowany klucz.

Uwagi

Jeśli comparer parametr ma nullwartość , domyślny moduł porównujący Default równości jest używany do porównywania kluczy.

Dotyczy

ToDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)

Tworzy słownik na podstawie wyliczenia zgodnie z określonym modułem porównującym klucz.

public:
generic <typename TKey, typename TValue>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::Dictionary<TKey, TValue> ^ ToDictionary(System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>> ^ source, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.Dictionary<TKey,TValue> ToDictionary<TKey,TValue> (this System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> source, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member ToDictionary : seq<System.Collections.Generic.KeyValuePair<'Key, 'Value>> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Value>
<Extension()>
Public Function ToDictionary(Of TKey, TValue) (source As IEnumerable(Of KeyValuePair(Of TKey, TValue)), comparer As IEqualityComparer(Of TKey)) As Dictionary(Of TKey, TValue)

Parametry typu

TKey

Typ kluczy z elementów elementu source.

TValue

Typ wartości z elementów elementu source.

Parametry

source
IEnumerable<KeyValuePair<TKey,TValue>>

Wyliczenie do utworzenia słownika.

comparer
IEqualityComparer<TKey>

Porównujący równość do porównywania kluczy.

Zwraca

Słownik zawierający klucze i wartości z sourceelementu .

Wyjątki

source jest odwołaniem null .

source zawiera co najmniej jeden zduplikowany klucz.

Uwagi

Jeśli comparer parametr ma nullwartość , domyślny moduł porównujący Default równości jest używany do porównywania kluczy.

Dotyczy

ToDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)

Tworzy słownik na podstawie wyliczenia zgodnie z domyślnym modułem porównującym dla typu klucza.

public:
generic <typename TKey, typename TValue>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::Dictionary<TKey, TValue> ^ ToDictionary(System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>> ^ source);
public static System.Collections.Generic.Dictionary<TKey,TValue> ToDictionary<TKey,TValue> (this System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> source);
static member ToDictionary : seq<System.Collections.Generic.KeyValuePair<'Key, 'Value>> -> System.Collections.Generic.Dictionary<'Key, 'Value>
<Extension()>
Public Function ToDictionary(Of TKey, TValue) (source As IEnumerable(Of KeyValuePair(Of TKey, TValue))) As Dictionary(Of TKey, TValue)

Parametry typu

TKey

Typ kluczy z elementów elementu source.

TValue

Typ wartości z elementów elementu source.

Parametry

source
IEnumerable<KeyValuePair<TKey,TValue>>

Wyliczenie do utworzenia słownika.

Zwraca

Słownik, który zawiera klucze i wartości z source i używa domyślnego modułu porównującego dla typu klucza.

Wyjątki

source jest odwołaniem null .

source zawiera co najmniej jeden zduplikowany klucz.

Dotyczy

ToDictionary<TKey,TValue>(IEnumerable<ValueTuple<TKey,TValue>>)

Tworzy słownik na podstawie wyliczenia zgodnie z domyślnym modułem porównującym dla typu klucza.

public:
generic <typename TKey, typename TValue>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::Dictionary<TKey, TValue> ^ ToDictionary(System::Collections::Generic::IEnumerable<ValueTuple<TKey, TValue>> ^ source);
public static System.Collections.Generic.Dictionary<TKey,TValue> ToDictionary<TKey,TValue> (this System.Collections.Generic.IEnumerable<(TKey Key, TValue Value)> source);
static member ToDictionary : seq<ValueTuple<'Key, 'Value>> -> System.Collections.Generic.Dictionary<'Key, 'Value>
<Extension()>
Public Function ToDictionary(Of TKey, TValue) (source As IEnumerable(Of ValueTuple(Of TKey, TValue))) As Dictionary(Of TKey, TValue)

Parametry typu

TKey

Typ kluczy z elementów elementu source.

TValue

Typ wartości z elementów elementu source.

Parametry

source
IEnumerable<ValueTuple<TKey,TValue>>

Wyliczenie do utworzenia słownika.

Zwraca

Słownik, który zawiera klucze i wartości z source i używa domyślnego modułu porównującego dla typu klucza.

Wyjątki

source jest odwołaniem null .

source zawiera co najmniej jeden zduplikowany klucz.

Dotyczy

ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)

Tworzy obiekt Dictionary<TKey,TValue>IEnumerable<T> na podstawie określonych funkcji selektora kluczy i selektora elementów.

public:
generic <typename TSource, typename TKey, typename TElement>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::Dictionary<TKey, TElement> ^ ToDictionary(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector);
public static System.Collections.Generic.Dictionary<TKey,TElement> ToDictionary<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
static member ToDictionary : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> -> System.Collections.Generic.Dictionary<'Key, 'Element>
<Extension()>
Public Function ToDictionary(Of TSource, TKey, TElement) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement)) As Dictionary(Of TKey, TElement)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez keySelector.

TElement

Typ wartości zwracanej przez elementSelector.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do utworzenia elementu Dictionary<TKey,TValue> na podstawie.

keySelector
Func<TSource,TKey>

Funkcja wyodrębniania klucza z każdego elementu.

elementSelector
Func<TSource,TElement>

Funkcja transformacji do uzyskiwania wartości wyniku elementu z każdego z elementu.

Zwraca

Dictionary<TKey,TElement>

Element Dictionary<TKey,TValue> zawierający wartości typu TElement wybrane z sekwencji danych wejściowych.

Wyjątki

source lub keySelectorelementSelector ma wartość null.

-lub-

keySelector polecenie tworzy klucz, który jest null.

keySelector tworzy zduplikowane klucze dla dwóch elementów.

Uwagi

Metoda ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) używa domyślnego modułu porównania Default równości do porównywania kluczy.

Dotyczy

ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)

Tworzy element Dictionary<TKey,TValue> na podstawie IEnumerable<T> określonej funkcji selektora kluczy, modułu porównującego i funkcji selektora elementów.

public:
generic <typename TSource, typename TKey, typename TElement>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::Dictionary<TKey, TElement> ^ ToDictionary(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.Dictionary<TKey,TElement> ToDictionary<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.Dictionary<TKey,TElement> ToDictionary<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member ToDictionary : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Element>
<Extension()>
Public Function ToDictionary(Of TSource, TKey, TElement) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement), comparer As IEqualityComparer(Of TKey)) As Dictionary(Of TKey, TElement)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez keySelector.

TElement

Typ wartości zwracanej przez elementSelector.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do utworzenia elementu Dictionary<TKey,TValue> na podstawie.

keySelector
Func<TSource,TKey>

Funkcja wyodrębniania klucza z każdego elementu.

elementSelector
Func<TSource,TElement>

Funkcja transformacji do uzyskiwania wartości wyniku elementu z każdego z elementu.

comparer
IEqualityComparer<TKey>

Element IEqualityComparer<T> do porównywania kluczy.

Zwraca

Dictionary<TKey,TElement>

Element Dictionary<TKey,TValue> zawierający wartości typu TElement wybrane z sekwencji danych wejściowych.

Wyjątki

source lub keySelectorelementSelector ma wartość null.

-lub-

keySelector polecenie tworzy klucz, który jest null.

keySelector tworzy zduplikowane klucze dla dwóch elementów.

Uwagi

Jeśli comparer parametr ma nullwartość , domyślny moduł porównujący Default równości jest używany do porównywania kluczy.

Dotyczy

ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

Tworzy obiekt Dictionary<TKey,TValue> na podstawie IEnumerable<T> określonej funkcji selektora klucza.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::Dictionary<TKey, TSource> ^ ToDictionary(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member ToDictionary : seq<'Source> * Func<'Source, 'Key> -> System.Collections.Generic.Dictionary<'Key, 'Source>
<Extension()>
Public Function ToDictionary(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As Dictionary(Of TKey, TSource)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez keySelector.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do utworzenia elementu Dictionary<TKey,TValue> na podstawie.

keySelector
Func<TSource,TKey>

Funkcja wyodrębniania klucza z każdego elementu.

Zwraca

Dictionary<TKey,TSource>

Obiekt Dictionary<TKey,TValue> zawierający klucze i wartości. Wartości w każdej grupie są w takiej samej kolejności, jak w pliku source.

Wyjątki

source lub keySelector ma wartość null.

-lub-

keySelector polecenie tworzy klucz, który jest null.

keySelector tworzy zduplikowane klucze dla dwóch elementów.

Przykłady

W poniższym przykładzie kodu pokazano, jak utworzyć ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)Dictionary<TKey,TValue> element przy użyciu selektora kluczy.

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
    public long TrackingNumber { get; set; }
}

public static void ToDictionaryEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2, TrackingNumber = 89453312L },
              new Package { Company = "Lucerne Publishing", Weight = 18.7, TrackingNumber = 89112755L },
              new Package { Company = "Wingtip Toys", Weight = 6.0, TrackingNumber = 299456122L },
              new Package { Company = "Adventure Works", Weight = 33.8, TrackingNumber = 4665518773L } };

    // Create a Dictionary of Package objects,
    // using TrackingNumber as the key.
    Dictionary<long, Package> dictionary =
        packages.ToDictionary(p => p.TrackingNumber);

    foreach (KeyValuePair<long, Package> kvp in dictionary)
    {
        Console.WriteLine(
            "Key {0}: {1}, {2} pounds",
            kvp.Key,
            kvp.Value.Company,
            kvp.Value.Weight);
    }
}

/*
 This code produces the following output:

 Key 89453312: Coho Vineyard, 25.2 pounds
 Key 89112755: Lucerne Publishing, 18.7 pounds
 Key 299456122: Wingtip Toys, 6 pounds
 Key 4665518773: Adventure Works, 33.8 pounds
*/
Structure Package
    Public Company As String
    Public Weight As Double
    Public TrackingNumber As Long
End Structure

Sub ToDictionaryEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With
      {.Company = "Coho Vineyard", .Weight = 25.2, .TrackingNumber = 89453312L},
      New Package With
      {.Company = "Lucerne Publishing", .Weight = 18.7, .TrackingNumber = 89112755L},
      New Package With
      {.Company = "Wingtip Toys", .Weight = 6.0, .TrackingNumber = 299456122L},
      New Package With
      {.Company = "Adventure Works", .Weight = 33.8, .TrackingNumber = 4665518773L}})

    ' Create a Dictionary that contains Package values,
    ' using TrackingNumber as the key.
    Dim dict As Dictionary(Of Long, Package) =
    packages.ToDictionary(Function(p) p.TrackingNumber)

    ' Display the results.
    Dim output As New System.Text.StringBuilder
    For Each kvp As KeyValuePair(Of Long, Package) In dict
        output.AppendLine("Key " & kvp.Key & ": " &
                      kvp.Value.Company & ", " &
                      kvp.Value.Weight & " pounds")
    Next
    Console.WriteLine(output.ToString())
End Sub

' This code produces the following output:
'
' Key 89453312: Coho Vineyard, 25.2 pounds
' Key 89112755: Lucerne Publishing, 18.7 pounds
' Key 299456122: Wingtip Toys, 6 pounds
' Key 4665518773: Adventure Works, 33.8 pounds

Uwagi

Metoda ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) używa domyślnego modułu porównania Default równości do porównywania kluczy.

Dotyczy

ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)

Tworzy element Dictionary<TKey,TValue>IEnumerable<T> na podstawie określonej funkcji selektora kluczy i modułu porównania kluczy.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::Dictionary<TKey, TSource> ^ ToDictionary(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member ToDictionary : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Source>
<Extension()>
Public Function ToDictionary(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IEqualityComparer(Of TKey)) As Dictionary(Of TKey, TSource)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ kluczy zwracanych przez keySelectorelement .

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do utworzenia elementu Dictionary<TKey,TValue> na podstawie.

keySelector
Func<TSource,TKey>

Funkcja wyodrębniania klucza z każdego elementu.

comparer
IEqualityComparer<TKey>

Element IEqualityComparer<T> do porównywania kluczy.

Zwraca

Dictionary<TKey,TSource>

Obiekt Dictionary<TKey,TValue> zawierający klucze i wartości. Wartości w każdej grupie są w takiej samej kolejności, jak w pliku source.

Wyjątki

source lub keySelector ma wartość null.

-lub-

keySelector polecenie tworzy klucz, który jest null.

keySelector tworzy zduplikowane klucze dla dwóch elementów.

Uwagi

Jeśli comparer parametr ma nullwartość , domyślny moduł porównujący Default równości jest używany do porównywania kluczy.

Dotyczy