C#.net:list<source> Turn dictionary<source.key,list<source>>

Source: Internet
Author: User

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}));}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.