c list partial operation implementation code

  • 2020-05-19 05:37:08
  • OfStack

C# Linq gets the intersection of the difference sets of two List or arrays


List<int> list1 = newList<int>();
list1.Add(1);
list1.Add(2);
list1.Add(3);
List<int> list2 = newList<int>();
list2.Add(3);
list2.Add(4);
list2.Add(5);
// And what you get is 4,5  I'm subtracting the same thing. 
List<int> list3 = list2.Except(list1).ToList();
foreach(inti inlist3)
{
MessageBox.Show(i.ToString());
}

Merge two arrays, remove duplicate elements, and sort (C#)


List<int> numbers1 = newList<int>() { 5, 4, 1, 3, 9, 8, 6, 7, 12, 10};
List<int> numbers2 = newList<int>() { 15, 14, 11, 13, 19, 18, 16, 17, 12, 10};
varnewQuerty = numbers1.Concat(
fromn innumbers2
where!numbers1.Contains(n)
selectn
).OrderBy(n=>n);

Merge the two arrays, remove the merged duplicate data, and sort


int[] A={1,2,2,3,4,5,6,6,6};
int[] B={2,2,2,3,7,8,9,5};
List<int> list = new List<int>(A);
list.AddRange(B);
list.Sort();
// Remove duplicates 
foreach (int i in list.Distinct<int>())
{
Console.WriteLine(i);
}

C# takes the same elements of two arrays
In the past, we were always thinking about cycles, element sizes, and everything. But now Linq is a good solution to this problem. Find the same item in two or more arrays.

The code is fairly simple:


usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceTest4_03
{
classProgram
{
staticvoidMain(string[] args)
{
string[] names = {"Adams","Arthur","Buchanan","Tsbuchis","ShCian","FuchsiaLinda","DecheChen","Lotheer","FindLanciCade","SorchLand","JiangZheng","MisiiLoda","Gtod","Dfac","Lama","BakCades","Losangle","ZheWQ","GehengDahaLothi","ToryLandey","DakaLothy","BthLanda","MenNorth","Fith","FoxMain","DontM","Saobba","Del","Sala","Ghero","BhthLaPhda"};
IEnumerable<string> skip = names.Skip(10);
IEnumerable<string> take = names.Take(11);
// Take out the intersection of two sequences, according to the theory should output JiangZheng
IEnumerable<string> intersect = skip.Intersect(take);
foreach(varsinintersect)
{
Console.WriteLine(s);
}
Console.ReadKey();
}
}
}

C# gets the difference set, intersection, of two array sets
Today in do a choice node in a tree, encounter a problem, an attribute node is recorded the related ID value, first to deal with these ID all, but then again choose will points, previously ID options if not in the collection of the new options out, otherwise, the original ID is not passed in processing, new ID passed in handling:

Such as the original
ID: 1,2,3,4 next time: 1,2,3,4,5.
ID is: 1,3. Next time: 3,4,5. 1.
ID is: 1,2,3,4,5. Next time: 3,4,5.
ID is: 1,2. Next time: 3,4,5.

1. Simplify the mathematical model:

You can see that this is actually a mathematical concept, the difference set of sets, so what do we do with it? Suppose the set of the previous selection is A, and the second selection is B

Getting what to do is easy: B-A (the difference set of B and A) is the set element to deal with. Why? According to the concept, ha!

So what if I get something that I don't do? You don't have to deal with a subset of B, so how do you get that?

Here we go: B-(B-A) why is that? B-A is to be processed, and what remains is, of course, B-(B-A),

What about the eliminated set? A - (B - (B - A))

How do you do it in C#? I'm not going to use loops or anything like that, I'm going to use NET 3.5 and that's easy to do. I'm going to use Linq:

Special point here, right click to get the tree set (lstSource) contains other information, first get the ID set again:


var m_ilAllSelect = lstSource.Select(r => r.ID).AsEnumerable();// List of new selections 
/////// So let's do that 
List<int> m_ilNewSelect = m_ilAllSelect.ToList();// New selection list 
List<int> m_ilExcept = m_ilNewSelect.Except(m_mcuids).ToList(); // The difference between the two 
List<int> m_iExceptAfterAndNew = m_ilNewSelect.Except(m_ilExcept).ToList();// The new selection list is compared to the difference set and is the old one in the new selection 

So just to simplify things for you, A for the old set, B for the new set, List for all of these < int > Generic list.
B.Except (A), B(B.Except (A)), A.Except (B(B.Except (A))),
Don't ask me what this Except method means. Look at MSDN. google will do!
Of course, my actual source code is more detailed than this point, so far to solve the set of the difference set of these points


Related articles: