C#.net uses ToDictionary (), GroupBy () to convert the list to dictionary, and the master needs a line of code!
First look at the demand, known as cars, equals:
List<car> cars = new list<car> () { new car (1, "audiA6", "private"), new car (2, "futon", "merchant"), New Car (3, "Feiren", "bike"), new car (4, "Bukon", "private"), new car (5, "Baoma", "private"), new car (6 , "Dayun", "Merchant") };
1) I would like to use the ID key, the value of car into a dictionary idcardict, the method is as follows:
var idcardict = cars. ToDictionary (car=>car.id);
This guarantees the correct conversion of the premise that the ID does not have duplicate values in the list. If there are duplicates, the exception that adds duplicate values to the dictionary is thrown.
2) I want to use the type as the key, the value of car's list of Typedict, the method is as follows:
dictionary<string, list<car>> typecardict = cars. GroupBy (car = car.type). ToDictionary (r = r.key, r = r.tolist ());
Explained in step:
First Step grouping
Cars. GroupBy (Car=>car.type)//Returns the result type://ienumerable<igroup<string,car>>;//where string equals Car.type, which is the grouped key
The second step is to convert the IEnumerable type to a dictionary, select the appropriate key,
ToDictionary (R=>r.key,r=>r.tolist ());//r parameter represents a grouped object, and R.key is the car.type;//r.tolist () action to convert a grouped object to a list object
The introduction of this conversion code is much simpler than the following foreach traversal to get a Car.type dictionary:
var dict = new dictionary<string,list<car>> (); foreach (Var Car in cars) { if (dict. Contains (Car.type)) Dict[car.type]. ADD (car); else Dict. ADD (car.type,new list<car> () {car}));}
C#.net uses ToDictionary (), GroupBy () to convert the list to dictionary, and the master needs a line of code!
First look at the demand, known as cars, equals:
List<car> cars = new list<car> () { new car (1, "audiA6", "private"), new car (2, "futon", "merchant"), New Car (3, "Feiren", "bike"), new car (4, "Bukon", "private"), new car (5, "Baoma", "private"), new car (6 , "Dayun", "Merchant") };
1) I would like to use the ID key, the value of car into a dictionary idcardict, the method is as follows:
var idcardict = cars. ToDictionary (car=>car.id);
This guarantees the correct conversion of the premise that the ID does not have duplicate values in the list. If there are duplicates, the exception that adds duplicate values to the dictionary is thrown.
2) I want to use the type as the key, the value of car's list of Typedict, the method is as follows:
dictionary<string, list<car>> typecardict = cars. GroupBy (car = car.type). ToDictionary (r = r.key, r = r.tolist ());
Explained in step:
First Step grouping
Cars. GroupBy (Car=>car.type)//Returns the result type://ienumerable<igroup<string,car>>;//where string equals Car.type, which is the grouped key
The second step is to convert the IEnumerable type to a dictionary, select the appropriate key,
ToDictionary (R=>r.key,r=>r.tolist ());//r parameter represents a grouped object, and R.key is the car.type;//r.tolist () action to convert a grouped object to a list object
The introduction of this conversion code is much simpler than the following foreach traversal to get a Car.type dictionary:
var dict = new dictionary<string,list<car>> (); foreach (Var Car in cars) { if (dict. Contains (Car.type)) Dict[car.type]. ADD (car); else Dict. ADD (car.type,new list<car> () {car}));}