. NET Framework-collections and "grouping" Technical Code in LINQ

Source: Internet
Author: User
We often have a collection of memory, such as a list, grouped according to one or some of the attributes, statistically displayed. The easiest way to do this is to iterate through a list instance to the following dictionary type, based on a key attribute.

Dictionary<string, list<myobject>

For example, cars are known,

                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")            };

To take the ID key, the value of car into a dictionary idcardict, in addition to traverse this logic is the most complex, the code needs the most methods, we can also directly use the ToDictionary method,

Ar idcardict = cars. ToDictionary (car=>car.id);

However, this method is limited, the key code corresponding to the object instance can only have one, that is, the return type is,

Dictionary<string,object>

This is bad, because there may be a key code corresponding to multiple instances, at this point you have to use GroupBy, by the key code group, and then into a dictionary.

For example, we want to use type as a key to get more than one car under this model,

dictionary<string, list<car>> typecardict = cars. GroupBy (car = car.type). ToDictionary (g = g.key, G = g.tolist ());

This conversion code is simple, much better than the following traversal logic!

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}));}

This solves a key code corresponding to multiple instances of the conversion, then according to the combination of multiple key codes corresponding to multiple instances of the problem, with the help of the list on the groupby can be achieved? cannot be achieved.

At this point, you need to write a LINQ statement that combines multiple key codes into a new object,

New {key1, Key2, ...}

For example, we have a collection where the elements in the collection are Valpair objects that contain two shaping elements, Val1 are small, and Val2 are relatively large. How do I group according to the combination of Val1,val2?
Take a look at the following logic:

        static void Printfduplicatecompare (List<valpair> compares)        {            //grouped by key combination, each group of elements greater than 2, sorted in descending order            var RtnByVal1 = from item in compares                                        group item by new {item. Val1, item. Val2} into                                            g                                            where G.count () >1 by                                G.count () descending                                select G;            Combination of Val1 and Val2 as the dictionary key, the number of elements is the value            var dict = rtnbyval1.todictionary (g = G.key,g=>g.count ());        }

Summarize

A list's groupby can only be grouped by one key, and if it needs to be grouped by multiple key combinations, a combination of LINQ statements must be written.

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.