Syntax:
1. If you wish to create a dictionary in keeping with specified key selector and ingredient selector features from the given information supply, overload the next methodology:
Parameters:
-
- supply: Supply will be an IEnumerable (listing) by which the dictionary is created from this supply.
- keySelector: It is a operate which is used to get the important thing from every ingredient.
- elementSelector: It is a operate which is used to get the ingredient.
- TSource: That is the kind parameter that specifies the kind of the information supply.
- TKey: That is the kind parameter that specifies the kind of the important thing.
- TElement: That is the kind parameter that specifies the kind of ingredient.
2. If you wish to create a dictionary in keeping with the desired key selector operate from the given information supply, overload the next methodology:
Parameters:
-
- supply: Supply will be an IEnumerable (listing) by which the dictionary is created from this supply.
- keySelector: It is a operate which is used to get the important thing from every ingredient.
- TSource: This refers to the kind of the information supply.
- TKey: It specifies the kind of key.
Instance 1: Specified Key Selector and Factor Selector Perform
Create a listing with the “Account” kind that holds 4 attributes (Acc_ID, Acc_Name, Business, Income) with 5 information.
1. Create a dictionary from the earlier listing with the important thing as Acc_ID and Worth as Acc_Name.
2. Create a dictionary from the earlier listing with the important thing as Acc_Name and Worth as Income.
utilizing System.Linq;
utilizing System.Collections.Generic;
class AccountInformation
{
public static void Most important()
{
// Create Listing of kind – Account.
Listing particulars = new Listing();
// Add 5 information to the Listing.
particulars.Add( new Account{Acc_ID= 1, Acc_Name = “Linux”, Business = “Training”,Income=2500});
particulars.Add( new Account{Acc_ID= 2, Acc_Name = “Python”, Business = “Bootcamp”,Income=10000});
particulars.Add( new Account{Acc_ID= 3, Acc_Name = “Java”, Business = “Training”,Income=500});
particulars.Add( new Account{Acc_ID= 4, Acc_Name = “.NET”, Business = “Coaching”,Income=2080});
particulars.Add( new Account{Acc_ID= 5, Acc_Name = “Oracle”, Business = “Job”,Income=2090});
// Create a Dictionary from the above Listing with Key as Acc_ID and Worth as Acc_Name
Console.WriteLine(” —-Key as Acc_ID and Worth as Acc_Name—-“);
Dictionary accounts_dict1 = particulars.ToDictionary(j => j.Acc_ID, j=> j.Acc_Name);
foreach (KeyValuePair i in accounts_dict1)
{
Console.WriteLine(“Account-Id :” + i.Key + ” Account-Title :” + i.Worth);
}
// Create a Dictionary from the above Listing with Key as Acc_Name and Worth as Income
Console.WriteLine(” —-Key as Acc_Name and Worth as Income—-“);
Dictionary accounts_dict2 = particulars.ToDictionary(l => l.Acc_Name, l=> l.Income);
foreach (KeyValuePair ok in accounts_dict2)
{
Console.WriteLine(“Account-Title :” + ok.Key + ” Account-Income :” + ok.Worth);
}
}
}
// Create class named – Account with 4 attributes
public class Account
{
public int Acc_ID { get; set; }
public string Acc_Name { get; set; }
public string Business { get; set; }
public int Income { get; set; }
}
Output:
Rationalization:
1. Create a category named “Account” with 4 attributes.
2. In the primary class, create listing of “Account” kind and insert 5 information into it.
3. Create a dictionary from the earlier listing with the important thing as Acc_ID and Worth as Acc_Name. Right here, we specify the TKey as int and the TElement as string. Contained in the ToDictionary() methodology, we move the Acc_ID within the keySelector and the Acc_Name within the elementSelector. Lastly, we use the “foreach” loop to iterate the dictionary and return the keys and values utilizing the important thing and worth properties.
4. Create a dictionary from the earlier listing with the important thing as Acc_Name and the worth as Income. Right here, we specify the TKey as string and the TElement as int. Contained in the ToDictionary() methodology, we move the Acc_Name within the keySelector and the Income within the elementSelector. Lastly, we use the “foreach” loop to iterate the dictionary and return the keys and values utilizing the important thing and worth properties.
Instance 2: Specified Key Selector
Use the earlier code and create a dictionary from the earlier listing with the important thing as Acc_ID.
utilizing System.Linq;
utilizing System.Collections.Generic;
class AccountInformation
{
public static void Most important()
{
// Create Listing of kind – Account.
Listing particulars = new Listing();
// Add 5 information to the Listing.
particulars.Add( new Account{Acc_ID= 1, Acc_Name = “Linux”, Business = “Training”,Income=2500});
particulars.Add( new Account{Acc_ID= 2, Acc_Name = “Python”, Business = “Bootcamp”,Income=10000});
particulars.Add( new Account{Acc_ID= 3, Acc_Name = “Java”, Business = “Training”,Income=500});
particulars.Add( new Account{Acc_ID= 4, Acc_Name = “.NET”, Business = “Coaching”,Income=2080});
particulars.Add( new Account{Acc_ID= 5, Acc_Name = “Oracle”, Business = “Job”,Income=2090});
// Create a Dictionary from the above Listing with Key as Acc_ID.
Dictionary accounts_dict = particulars.ToDictionary(j => j.Acc_ID);
foreach (KeyValuePair i in accounts_dict)
{
Console.WriteLine(“Key:” + i.Key + “–> Account-Title :” + i.Worth.Acc_Name
+ ” Account-Business :” + i.Worth.Business
+ ” Account-Income :” + i.Worth.Income);
}
}
}
// Create class named – Account with 4 attributes
public class Account
{
public int Acc_ID { get; set; }
public string Acc_Name { get; set; }
public string Business { get; set; }
public int Income { get; set; }
}
Output:
Rationalization:
Right here, we create a dictionary with the important thing as Acc_ID. This acts as the important thing for every document within the dictionary that we created from the listing. After that, we use the “foreach” loop to get the keys and values (with attributes) utilizing the important thing and worth properties.
Instance 3: Duplicate Keys – ArgumentException
Create a listing with two information and attempt to convert it right into a dictionary with the Acc_ID as key.
utilizing System.Linq;
utilizing System.Collections.Generic;
class AccountInformation
{
public static void Most important()
{
// Create Listing of kind – Account.
Listing particulars = new Listing();
// Add 2 information to the Listing.
particulars.Add( new Account{Acc_ID= 1, Acc_Name = “Linux”, Business = “Training”,Income=2500});
particulars.Add( new Account{Acc_ID= 1, Acc_Name = “Python”, Business = “Bootcamp”,Income=10000});
// Attempt to create a Dictionary from the above Listing with Key as Acc_ID.
Dictionary accounts_dict = particulars.ToDictionary(j => j.Acc_ID);
}
}
// Create class named – Account with 4 attributes
public class Account
{
public int Acc_ID { get; set; }
public string Acc_Name { get; set; }
public string Business { get; set; }
public int Income { get; set; }
}
Exception:
An unhandled exception which is System.ArgumentException is raised since the secret is duplicate (1) in each the Acc_ID’s.
Instance 4: Null Supply – ArgumentNullException
Create a listing with the “Account” kind and assign null to it. Attempt to create a dictionary from the earlier listing with the important thing as Acc_ID.
utilizing System.Linq;
utilizing System.Collections.Generic;
class AccountInformation
{
public static void Most important()
{
// Create Listing of kind – Account and assign null to it.
Listing particulars = null;
// Attempt to create a Dictionary from the above Listing with Key as Acc_ID.
Dictionary accounts_dict = particulars.ToDictionary(j => j.Acc_ID);
}
}
// Create class named – Account with 4 attributes
public class Account
{
public int Acc_ID { get; set; }
public string Acc_Name { get; set; }
public string Business { get; set; }
public int Income { get; set; }
}
Exception:
An unhandled exception which is System.ArgumentNullException is raised because the listing is null.
Conclusion
We realized the way to create a dictionary from the IEnumerable (right here, it’s listing) utilizing the ToDictionary() methodology in C# LINQ. This methodology will be overloaded in two methods. We mentioned each strategies with examples. Additionally, we realized the 2 exception circumstances which are raised by this methodology when the information supply/ keySelector/ elementSelector is null and the keys are duplicate.