C# 针对List去重处理
1 static public List<List<string>> DistinctList(List<List<string>> OriginalListCrpcps) 2 { 3 List<string> WholeFrm = new List<string>(); 4 List<List<string>> DistinctListCrpcps = new List<List<string>>(); 5 foreach (List<string> Original in OriginalListCrpcps) 6 { 7 foreach (string ori in Original) 8 { 9 if (!WholeFrm.Contains(ori)) 10 { 11 WholeFrm.Add(ori); 12 } 13 } 14 if (DistinctListCrpcps.Count() == 0) 15 { 16 DistinctListCrpcps.Add(Original); 17 continue; 18 } 19 bool cur = false; 20 foreach (List<string> Distinct in DistinctListCrpcps) 21 { 22 foreach (var Ori in Original) 23 { 24 if (!Distinct.Contains(Ori)) 25 { 26 cur = true; 27 } 28 } 29 } 30 if (cur) 31 { 32 DistinctListCrpcps.Add(Original); 33 } 34 } 35 //所有的切片,如果这个切片被其他切片包含,那么删除这个切片, 36 //这个切片里面所有的东西都在另一个切片里面 37 List<List<string>> SimpleDistinctListCrpcps = new List<List<string>>(); 38 foreach (var Dis in DistinctListCrpcps) 39 { 40 foreach (var DisOther in DistinctListCrpcps) 41 { 42 //除了本身,如果这个的所有东西都在另一个切片里面,那么这个就要删除 43 if ((Dis != DisOther) && (Dis.All(x => DisOther.Contains(x)))) 44 { 45 SimpleDistinctListCrpcps.Add((Dis)); 46 } 47 } 48 } 49 foreach (var sim in SimpleDistinctListCrpcps) 50 { 51 DistinctListCrpcps.Remove(sim); 52 } 53 return DistinctListCrpcps; 54 }