C# List集合中獲取重復值及集合運算詳解
話不多說,直接上實例:
一、獲取集合內重復值
public void GetDuplicateValue()
{
List<string> lisA = new List<string> { "A", "B", "C", "A" };
//方式一 借助字典
Dictionary<string, int> dic = new Dictionary<string, int>();
lisA.ForEach(x =>
{
if (dic.ContainsKey(x))
dic[x] += 1;
else
dic[x] = 0;
});
List<string> lisDupValues = dic.Where(x => x.Value > 0).Select(x => x.Key).ToList(); //結果{"A"}
//方式二
List<string> lisDupValues2 = lisA.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); //結果{"A"}
//方式三 等同于方式二
List<string> lisDupValues3 = (from r in lisA group r by r into g where g.Count() > 1 select g.Key).ToList(); //結果{"A"}
}
由上述可看出方式二、三的寫法非常簡潔。便去Microsoft官網(wǎng)了解下了,又發(fā)現(xiàn)了許多新的東西,Linq還真是挺好用的
二、單個集合操作
1、All、Any
public void All_Any()
{
List<string> lisA = new List<string> { "A", "B", "C", "A" };
//All:確定序列中的所有元素是否都滿足條件
bool all = lisA.All(x => x.Equals("B")); //結果 false
//Any:確定序列中的任何元素是否存在或滿足條件。
bool any = lisA.Any(x => x.Equals("B")); //結果 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:計算數(shù)值序列的和。
double sum = lisA.Sum(); //結果 8
//Average:計算數(shù)值序列的平均值。
double average = lisA.Average(); //結果 2
//Distinct:返回序列中的非重復元素
List<int> distinctLisA = lisA.Distinct().ToList(); //結果 {1,2,3}
//Max:返回值序列中的最大值。
double max = lisA.Max(); //結果 3
//Min:返回值序列中的最小值。
double min = lisA.Min(); //結果 1
//Select:將序列中的每個元素投影到新表單。
var query = lisA.Select((age, index) => new { index, jn = age + 1 }); //結果:{index=0,jn=2},{index=1,jn=3},{index=2,jn=3},{index=3,jn=4}
//Skip:跳過序列中指定數(shù)量的元素,然后返回剩余的元素。
List<int> lowerGrades = lisA.Skip(3).ToList(); //結果 {3}
//Take:從序列的開頭返回指定數(shù)量的相鄰元素。
List<int> task = lisA.Take(2).ToList(); //結果 {1,2}
//ToDictionary:根據(jù)指定的鍵選擇器函數(shù)、比較器和元素選擇器函數(shù),從 IEnumerable<T> 創(chuàng)建一個 Dictionary<TKey,TValue>。
var dic = lisA.Distinct().ToDictionary(x => x); //結果 {{1,1},{2,2},{3,3}}
}
三、集合間運算
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:連接兩個序列。
List<string> query = lisA.Concat(lisB).ToList(); //結果 { "A", "B", "C", "A" ,"A", "B", "H", "K"}
//Except:生成兩個序列的差集。
List<string> onlyInLisASet = lisA.Except(lisB).ToList(); //結果 {"C"}
//Intersect:生成兩個序列的交集。
List<string> duplicates = lisA.Intersect(lisB).ToList(); //結果 {"A","B"}
//Union:生成兩個序列的并集。
List<string> union = lisA.Union(lisB).ToList(); //結果 { "A", "B", "C", "H", "K"}
//Zip:將指定函數(shù)應用于兩個序列的對應元素,以生成結果序列。
List<string> zip=lisA.Zip(lisB, (first, second) => first + " " + second).ToList(); //結果 { "A A", "B B", "C H", "A K" }
}
補充知識:c#中List的元素遍歷(foreach)和去重復(distinct)
一、準備工作
定義實體類people
public List<People> PeopleList { get; set; }
public class People
{
public string Name { get; set; }
public int Age { get; set; }
}
實體比較help類
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 obj.ToString().GetHashCode();
}
}
二、List.ForEach()
假設需要對集合中的每個元素進行運算(將每個人的年齡增加10歲)
PeopleList.ForEach(p=>{
p.Age = p.Age + 10;
});
三、List.Distinct()
假設需要將姓名和年齡相同的元素過濾掉
PeopleList.Distinct(new Common.List.ListCompare<People>(
(x,y)=> x.Name==y.Name&&x.Age==y.Age)
);
解析:
ListCompare類用來比較List中的兩個元素。它的構造函數(shù)中需要傳入一個CompareDelegate。
可以看出,兩個元素的比較,重點在CompareDelegate中。
定義: public delegate bool CompareDelegate(T x, T y);
其實,ListCompare實現(xiàn)了IEqualityComparer接口。
以上這篇C# List集合中獲取重復值及集合運算詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

