How to use Find, findall, and sort in a collection class

Source: Internet
Author: User
Tags bool

The most recent development is based on. NET 2.0,.net 2.0 is classic, but with LINQ and Lambda, it's now known how powerful and great satisfaction LINQ and Lambda are, especially for collection classes.

I thought in the 2.0 era, for the collection class we can only write cycle to its screening or sorting, and so on, it is too short-sighted.

OK, cut the crap and get to the point. The first share of the code is the Find method, its parameters for a delegate, Microsoft has defined this delegate for us, direct use can be. (If you do not understand the delegate, you can Google, or refer to the http://www.cnblogs.com/FreeDong/archive/2012/09/27/2705372.html do C # Open If you know how to use delegates and events that will make our coding world richer and more interesting, well, put in the code of a method I wrote:

<summary>
        ///Method Description: Whether to include the menu name
        ///    : Liangwendong
        ///creation time: 2012-12-14
        ///</summary>
        <param name= "key" >the key.</param>
        ///<returns> return contains the menu name </returns> public
        BOOL Cantainkey (string key)
        {return this
            . Menuedits.find (New predicate<menueditentity> delegate

(menueditentity entity)
            {return
                Entity. Menukey = = key;
            }) != null;
        }

This in the method. Menuedits is a member of the class, the type is list<menueditentity> I used the anonymous delegate directly here, and the delegate returns the bool type, which is the condition we want to filter.

Next to the FindAll method, however, I don't post the code, the usage is similar to find, except that it returns the set object that matches the criteria, not just the first one.

And then to the sort method, this is a bit cumbersome, first look at the code.

List. Sort (New 

comparison<baseeditor> delegate (Baseeditor Model1, Baseeditor model2) 
                {
                    if (model1). TabIndex = = Model2. TabIndex)
                    {return
                        0;
                    }
                    else if (model1. TabIndex < Model2. TabIndex)
                    {
                        return-1;
                    }
                    else
                    {return
                        1;
                    }
                }));

The same type of list is List<baseeditor>, and Baseeditor is a custom control (the control must contain the TabIndex property, hehe), I'm going to get all the controls in a form here, and then I want these controls to According to the TabIndex to do the sort, this does the form development to use also many AH. Then there is a difference in the sort method that does not return a value and sorts the list directly.

Finally this delegate is a bit difficult to understand, here the return value of only-1, 0, 1, the other return value to the sort has no effect, return 0 and 1 of two of the objects are not in the order, if the return of 1 will need to be adjusted, as above,

Model1. TabIndex > Model2. TabIndex indicates that Model1 should be ranked after Model2, and so on.

Of course, there are other uses of the more extensive overload, we can see the specific MSDN, I only as a brick, look forward to the jade!!

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.