Small issues that are easy to ignore when using the LINQ Extension Method

Source: Internet
Author: User

Problem Reproduction

The complete code used to illustrate the topic of the article is provided below.

//************************************** ********************* // Sample code of the LINQ Extension Method /// Author: may 3, May /// Date: //// ////****************************** * ***************************** using system; using system. collections. generic; using system. LINQ; namespace linqextendfunctionexp {class program {static void main (string [] ARGs) {list <student> studentlist = new list <student> (); studentlist. add (new student () {id = 1, name = "zhangsan", classid = 1, score = 85}); studentlist. add (new student () {id = 2, name = "Lisi", classid = 2, score = 76}); studentlist. add (new student () {id = 3, name = "wangwu", classid = 1, score = 89}); studentlist. add (new student () {id = 4, name = "zhaoliu", classid = 2, score = 91}); studentlist. add (new student () {id = 5, name = "liujian", classid = 1, score = 78}); studentlist. add (new student () {id = 6, name = "Wubin", classid = 2, score = 67}); var handledstudentlist = studentlist. groupby (IT => it. classid ). orderby (IT => it. max (P => P. score )). tolist (); foreach (VAR students in handledstudentlist) {foreach (VAR student in students) {If (student. score <85) {student. name = "unqualified" ;}}}foreach (VAR s in studentlist) {console. writeline ("ID = {0}, name = {1}, classid = {2}, score = {3}", S. ID, S. name, S. classid, S. score) ;}} public class student {public int ID {Get; set;} public string name {Get; set;} public int classid {Get; set ;} public int score {Get; Set ;}}}
The Code mainly completes the following tasks:

(1) define studentlist;

(2) Use the LINQ Extension Method to group and sort the studentlist set to obtain the set handledstudentlist;

(3) traverse the student object in the handledstudentlist set. When the student's score is lower than 85, change the student's name to "unqualified ";

(4) output the information of all students in the studentlist set.

The problems to be discussed in this article are:

When studentlist set student information is output in operation (4), if the score of students whose IDs are 2, 5, and 6 is lower than 85, will these students' names be displayed as "unqualified?

You may say no and give your reasons at the same time:

Because the operation to change the Student name to "unqualified" is completed when traversing the handledstudentlist set, the student information in the handledstudentlist set is modified, therefore, studentlist is not affected when the studentlist set is output.

But is it true? See the code execution result.


Figure 1 code running result

As shown in figure 1, we can see that the names of students whose student IDs are 2, 5, and 6 are changed to "unqualified". Why?


Problem Analysis

The following uses the groupby extension method as an example to analyze the problem. The use of other extension methods in LINQ is similar to the use of the Extension Method groupby.

The scaling method groupby is used to group A set according to the specified conditions. A groupedenumerable object is returned, and the returned object still holds the reference of the group set. Therefore, when we operate on the set after the group, we still operate on the set object before the group. For this reason, the phenomenon described above has emerged.

Isn't it easy to ignore this, because it clearly returns a new type of object, who will think that it still holds a reference to the processed set inside it.

Well, I hope you will pay attention to this when using the extension method of LINQ in the future.

As to why the groupby method is implemented in this way, you have to study the source code of the groupby method. Wait till you have time to talk about it. I just want to raise this question for your reference, to avoid some minor problems that should not have occurred because you do not know this knowledge point.

Okay, 88.



Small issues that are easy to ignore when using the LINQ Extension Method

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: 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.