. Net (C #): Be alert to the sequeness of Plinq results

Source: Internet
Author: User

The running result of Plinq is unordered, that is, it does not keep the sequence of the original set for operation (except for some special sorting operations ). The reason is that the concurrent execution of the thread is full of uncertainty. Splitting the original task into several parts and returning results at the same time will disrupt the original order. If you want to retain the order forcibly, some performance will be wasted. Plinq can be like this, but it is not like this by default.

 

First, let's look at a LINQ example:

VaRArr= New Int[] {1,2,3,4,5};

VaRRes=Arr.Where (I=>I! = 3);

The result is:

1, 2, 4, 5

 

The order is maintained. Plinq:

VaRRes=Arr.Asparallel ().Where (I=>I! = 3);

 

The order of results has changed:

1, 4, 5, 2

 

 

Use Plinq's asordered and asunordered commands to execute Plinq to retain the original set sequence or not.

Change the above Plinq:

VaRRes=Arr.Asparallel ().Asordered ().Where (I=>I! = 3);

The result is: 1, 2, 4, 5.

 

However, if the Plinq set is sorted, subsequent operations will automatically keep the order, just as asordered is called.

VaRRes=Arr.Asparallel ().Orderby (I=>I).Where (I=>I! = 3);

The result is: 1, 2, 4, 5.

 

Note that some Plinq methods are of little significance if they are not executed sequentially, such as the reverse method:

VaRRes=Arr.Asparallel ().Reverse ();

 

The result is:

5, 3, 2, 1, 4

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.