The aggregate of LINQ

Source: Internet
Author: User

Objective

LINQ about common methods we have played handy, and for those less use is ignored (exaggerated point), but only in the practical application of the method is not as good as the built-in LINQ method to the actual and concise, do not like to spray, blame me for short.

Introducing topics through Ruby

Before showing a piece of code in Ruby to tell the magic of Ruby, let's look at the following:

sum = 0 (1..101). Each do|elem|   sum+ =I  i+=1endputs sum

Yes this is the code, 1 to 100, and the code is beautiful and concise let me marvel.

Continue to introduce topics through C #

If you are a person with more than two years of work experience, when in an interview, the interviewer will give you such a problem: please code to find 1 to 100 and, in two or more or more concise method to achieve. Are you going to write it out soon? Let's take a look.

The first
            var 0 ;              for (int1; i++)            {                + = i;            }            Console.WriteLine (sum);        

One go, this can be difficult to have two years of work experience above the people, the very sweet coax. Nonstop, walk you, and proceed to the second way.

The second
        Static int Sum ()        {            var0;             var 1 ;              Do             {                + = i;                I++            ;  while  - );             return sum;        }

Oh, sure enough speed, since all have do while, not to say to two or more ways, continue to one.

The third
        Static int Sum ()        {            var0;             var 1 ;              while  - )            {                + = i;                I++            ;             } return sum;        }

Well, to this even after the finished, gently loose on the OK, the interviewer a look, continue to ask you, in addition to these there are other ways, you decisively said no, at this time the interviewer said basically satisfied, sorry, can only give you a pass, you are not wondering? What, you said two or more of the methods I have achieved and is the most concise way ah, and the results are right, will you think the interviewer is contradicting with you?

If that's what I would have done before, I've always believed that this is the only way to achieve 1 to 100 in C # (and, of course, with do and while), and we need to think about what the interviewer's intentions are, to test you, At least also work for more than two years of people, it is simply look down on people, yes indeed a little despise people, as the work experience of the people, you test me this, yes, it is because the work experience is enough to test you this, but you did not reflect your ability. OK, donuts, we started to get to the point, at this time you have found the code is still a bit cumbersome, C # Concise syntax where to go, let me start on my favorite C # a bit not firm.

Aggregate

This is a topic we haven't introduced for a long while, and using it we can write code that is as beautiful as Ruby, and we'll see how to find 1 to 100 of the same:

            var list = Enumerable.range (1);             var result = list. Aggregate ((a, B) = (A + b));            Console.WriteLine (string. Format ("1 to 100 and {0}", result));

It is so easy, let me bright, and full of confidence in C #, as mentioned above, as a person with work experience, probably this method is not how to use, most of the common LINQ, of course, so many LINQ can all use Ah, but, But at least you need to know what this LINQ is about (Don't go in your heart, say it to me).

Usage

The LINQ literal meaning is aggregation, for example, it can be used to filter the collection and the sum above, and so on, it has three overloads, it is to each element on the list to perform operations, that is, the first and second elements on the operation, the execution of the results continue to carry out the operation. One of the overloads is a seed reload.

Seed reload (seed overload)

We ask for factorial from 1 to 5.

            var nums = Enumerable.range (24);             var sum = nums. Aggregate (1, (A, B) and A * b);            Console.WriteLine (sum);

The so-called seed, that is, start from the seed as the first element to perform operations, the entire process is (((1*2) *).

Conclusion

I didn't have much contact with aggregate, in the project to see someone else write such a piece of code just let me subconsciously know to this LINQ completely did not contact, so there is this part of the content. There is a scene in the project: Open the dialog to upload a file, you can name it, but we know that the file name is not named after some special characters such as < or | And so on, when the file name is these, upload to the background we will replace processing with "-" to replace. Let's look at the code, for example:

            var invalidfilename = path.getinvalidfilenamechars ();             var replaceresult = invalidfilename.aggregate ("study<aggregate>first" ' - ' )));            Console.WriteLine (Replaceresult);

The results are as follows:

Aggregate study finished, Sleep now!

The aggregate of LINQ

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.