C # Two List generics use LINQ to repeat data or to get duplicate data

Source: Internet
Author: User

1, two list generics use LINQ to repeat data

Code:

list<string> Lsta =Newlist<string> {"e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568" }; List<string> LSTB =Newlist<string> {"e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568","e00401501b652569" }; List<string> newlist = lsta.findall (x =!)lstb.contains (x)); foreach(varIteminchnewlist)            {Console.WriteLine (item); } console.readkey ();

Console Run Results

Figure 1

Figure 1 Why this is happening and nothing is output. Below to modify the next program, see 2

2, first look at Code:

list<string> Lsta =Newlist<string> {"e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568" }; List<string> LSTB =Newlist<string> {"e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568","e00401501b652569" }; List <string> NewList = lstb.findall (x =! ) Lsta.contains (x)); foreach(varIteminchnewlist)            {Console.WriteLine (item); } console.readkey ();

Look at the results again:

Figure 2

Look at the code of the bold line and figure 2 results, you must see why.

The array of a is treated as B-interval, the range of B is greater than a interval, and the public parts of a and B are removed, and the different parts are found out.

1 Results There is no data output, the reason is here.

3, in the A array in the addition of a MAC address at this time a, b two range of the same size

Code

list<string> Lsta =Newlist<string> { "e00401501b652562" ,"e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568" }; List<string> LSTB =Newlist<string> {"e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568","e00401501b652569" }; List<string> newlist = lstb.findall (x =!)lsta.contains (x)); foreach(varIteminchnewlist)            {Console.WriteLine (item); } console.readkey ();

Look at the results again:

Figure 3

As shown in Figure 3 and Figure 2, the data that is not duplicated should be the e00401501b652562,e00401501b652569 two MAC addresses correctly.

How does the code adjust? See 4

4,code

list<string> Lsta =Newlist<string> {"e00401501b652563","e00401501b652562","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568" }; List<string> LSTB =Newlist<string> {"e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568","e00401501b652569" }; List<string> Listall =Newlist<string>(); List<string> Listresult =Newlist<string>(); varListunionall = Lsta.union (LSTB). (t = t);//Sort merge Data                        foreach(varIteminchListunionall)            {Listall.add (item); } List<string> newlist = listall.findall (x =!lsta.contains (x));//to repeat, combine a new list collectionlist<string> newList2 = listall.findall (x =!)lstb.contains (x)); varUnionlist = Newlist.union (newList2). (t = t);//Sort merge Data            foreach(varIteminchunionlist)            {Console.WriteLine (item); } console.readkey ();

Results

5, find the duplicate data to the LINQ is not equal to remove the line.

list<string> Lsta =Newlist<string> {"e00401501b652562","e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568" }; List<string> LSTB =Newlist<string> {"e00401501b652563","e00401501b652564","e00401501b652565","e00401501b652566","e00401501b652567","e00401501b652568","e00401501b652569" }; List<string> newlist = lstb.findall (x =lsta.contains (x)); foreach(varIteminchnewlist)            {Console.WriteLine (item); } console.readkey ();

Results

MAC addresses 62 and 69 have no output.

C # Two List generics use LINQ to repeat data or to get duplicate data

Related Article

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.