If you want to compare and process data in the process of using LINQ, remember to use the tolist () method.

Source: Internet
Author: User
1 Private   Void Simplebutton#click ( Object Sender, eventargs E)
2 {
3 List < Testclass > List =   New List < Testclass > ();
4 Int [] Ary =   New   Int [] { 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 4 };
5
6 For ( Int I =   0 ; I <   100 ; I ++ )
7 {
8 List. Add ( New Testclass (I, I ));
9 }
10
11 VaR list2 = From t In List
12 Select New Testclass (T. x, t. y );
13
14 Foreach (VAR t In List2)
15 {
16 While ( True )
17 {
18 MessageBox. Show (T. gethashcode (). tostring () +   " : "   + List2.tolist ()[ 0 ]. Gethashcode (). tostring ());
19 MessageBox. Show (list2.tolist (). indexof (T). tostring ());
20 Return ;;
21 }
22 }
23
24 }
25
26 Public   Class Testclass
27 {
28 Public Testclass ( Int X, Int Y)
29 {
30 This . X = X;
31 This . Y = Y;
32 }
33
34 Int X;
35 Public   Int X
36 {
37 Get
38 {
39 Return X;
40 }
41 Set
42 {
43 X = Value;
44 }
45 }
46
47 Int Y;
48 Public   Int Y
49 {
50 Get
51 {
52 Return Y;
53 }
54 Set
55 {
56 Y = Value;
57 }
58 }
59 }
60

The gethashcode values are different. The retrieval result is-1. It is reasonable to say no. I interpret the objects in list2 from the list2 set.

Key Code If it is changed to the following method, you can save the result to a collection first. 1 VaR list2 = From t In List
2 Select New Testclass (T. x, t. y );
3 VaR list3 = List2.tolist ();
4 Foreach (VAR t In List3)
5 {
6 While ( True )
7 {
8 MessageBox. Show (T. gethashcode (). tostring () +   " : "   + List3.tolist ()[ 0 ]. Gethashcode (). tostring ());
9 MessageBox. Show (list3.tolist (). indexof (T). tostring ());
10 Return ;;
11 }
12 }
13

I don't know the reason, but I found this problem. If the man knows it, I will post it. Thank you.

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.