Without further ado, just go to the example:
1. Get duplicate values in the set
public void GetDuplicateValue() { List<string> lisA = new List<string> { "A", "B", "C", "A" }; //Method 1 With the help of dictionary Dictionary<string, int> dic = new Dictionary<string, int>(); (x => { if ((x)) dic[x] += 1; else dic[x] = 0; }); List<string> lisDupValues = (x => > 0).Select(x => ).ToList(); //Result {"A"} //Method 2 List<string> lisDupValues2 = (x => x).Where(x => () > 1).Select(x => ).ToList(); //Result {"A"} //Method 3 is equivalent toMethod 2 List<string> lisDupValues3 = (from r in lisA group r by r into g where () > 1 select ).ToList(); //Result {"A"}}
From the above, we can see that the writing of methods 2 and 3 is very concise. So I went to the Microsoft official website to find out and found a lot of new things. Linq is really easy to use
2. Single set operation
1、All、Any
public void All_Any() { List<string> lisA = new List<string> { "A", "B", "C", "A" }; //All: Determine whether all elements in the sequence meet the conditions bool all = (x => ("B")); //Result false //Any: Determines whether any element in the sequence exists or meets the conditions. bool any = (x => ("B")); //Result true}
2、Sum、Average、Distinct、Max、Min、Skip、Take、ToDictionary
public void Sum_Average_Distinct_Max_Min_Skip_Take_ToDictionary() { List<int> lisA = new List<int> { 1, 2, 2, 3 }; //Sum: Calculate the sum of the numerical sequence. double sum = (); //Result 8 //Average: Calculate the average value of the numerical sequence. double average = (); //Result 2 //Distinct: Returns non-repetitive elements in the sequence List<int> distinctLisA = ().ToList(); //Result {1,2,3} //Max: Return the maximum value in the value sequence. double max = (); //Result 3 //Min: Return the minimum value in the value sequence. double min = (); //Result 1 //Select: Project each element in the sequence to a new form. var query = ((age, index) => new { index, jn = age + 1 }); //Result: {index=0,jn=2},{index=1,jn=3},{index=2,jn=3},{index=3,jn=4} //Skip: Skip the specified number of elements in the sequence and return the remaining elements. List<int> lowerGrades = (3).ToList(); //Result {3} //Take: Returns a specified number of adjacent elements from the beginning of the sequence. List<int> task = (2).ToList(); //Result {1,2} //ToDictionary: Create a Dictionary<TKey,TValue> from IEnumerable<T> according to the specified key selector function, comparator and element selector function. var dic = ().ToDictionary(x => x); //Result {{1,1},{2,2},{3,3}}}
3. Inter-set operations
1、Concat、Except、Intersect、Union、Zip
public void Concat_Except_Intersect_Union_Zip() { List<string> lisA = new List<string> { "A", "B", "C", "A" }; List<string> lisB = new List<string> { "A", "B", "H", "K" }; //Concat:Concatenate two sequences. List<string> query = (lisB).ToList(); //Result { "A", "B", "C", "A" ,"A", "B", "H", "K"} //Except: Generate the difference set of two sequences. List<string> onlyInLisASet = (lisB).ToList(); //Result {"C"} //Intersect: Generate the intersection of two sequences. List<string> duplicates = (lisB).ToList(); //Result {"A","B"} //Union: Generate a union of two sequences. List<string> union = (lisB).ToList(); //Result { "A", "B", "C", "H", "K"} //Zip: Apply the specified function to the corresponding elements of the two sequences to generate the result sequence. List<string> zip=(lisB, (first, second) => first + " " + second).ToList(); //Result { "A A", "B B", "C H", "A K" }}
Supplementary knowledge:Foreach and deduplication of List elements in c#
1. Preparation
Define entity class people
public List<People> PeopleList { get; set; } public class People { public string Name { get; set; } public int Age { get; set; } }
Entity comparison help class
public delegate bool CompareDelegate<T>(T x, T y); public class ListCompare<T> : IEqualityComparer<T> { private CompareDelegate<T> _compare; public ListCompare(CompareDelegate<T> d) { this._compare = d; } public bool Equals(T x, T y) { if (_compare != null) { return this._compare(x, y); } else { return false; } } public int GetHashCode(T obj) { return ().GetHashCode(); } }
two,()
Suppose that you need to perform operations on each element in the set (increase each person's age by 10 years)
(p=>{ = + 10; });
three,()
Assume that elements with the same name and age need to be filtered out
(new <People>( (x,y)=> ==&&==) );
Analysis:
ListCompare class is used to compare two elements in List. Its constructor needs to pass a CompareDelegate.
It can be seen that the comparison of the two elements focuses on CompareDelegate.
Definition: public delegate bool CompareDelegate(T x, T y);
In fact, ListCompare implements the IEqualityComparer interface.
The detailed explanation of the above C# List collection is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.