1 List<string> l1 = "1,2,3,4,5,6,7,8,9,0".Split(',').ToList(); 2 List<string> l2 = "1,2,3,4,5,6,a,b,c,d,e".Split(',').ToList(); 3 List<string> l3 = "1,2,3".Split(',').ToList(); 4 List<string> l4= "1,2,3".Split(',').ToList(); 5 List<string> l5 = "111,111,2222,3333,4444,5555,6666,6,2,1,54".Split(',').ToList(); 6 //交集 123456 7 List<string> bing = l1.Intersect(l2).ToList(); 8 //并集(不排除重复元素) 1234567890123456abcde 9 List<string> bingNoDistinct = l1.Concat(l2).ToList(); 10 11 //并集(排除重复元素) 1234567890abcde 12 List<string> bingDistinct = l1.Union(l2).ToList(); 13 14 //及格去重 15 List<string> list = l5.Distinct().ToList(); 16 17 //所有的元素是否全部满足条件 l2集合中是否全部包含l1中的元素 18 bool a = l1.All(x => l2.Contains(x)); 19 20 //集合中是否有满足某些条件的元素 l1集合中是否包含“1” 21 bool b = l1.Any(x=>x=="1"); 22 23 //集合1是否完整包含集合2 l1是否完整包含l3 24 bool c = l3.All(x => l1.Any(y => x.Equals(y))) ; 25 bool d = l3.All(x => l1.Any(y => x==y)); 26 bool i = l3.All(x => l1.Contains(x)); 27 28 //两个集合是否相等 l4是否包含l3,并且两个count相等 29 bool f = l3.All(x => l4.Contains(x)) && l3.Count == l4.Count; 30 31 //根据条件返回集合 32 List<string> listCondition = l5.Where(x => x.Length == 2).ToList();
将一个list集合分为等额的子集合
/// <summary> /// 将一个集合分为多个集合 /// </summary> /// <typeparam name="T">类</typeparam> /// <param name="tList">大集合</param> /// <param name="perInt">每一个集合的个数</param> /// <returns></returns> private static List<List<T>> SeperateList<T>(List<T> tList,int perInt) { List<List<T>> llist = new List<List<T>>(); int j = perInt; for (int i = 0; i < tList.Count; i += perInt) { List<T> cList = new List<T>(); cList = tList.Take(j).Skip(i).ToList(); j += perInt; llist.Add(cList); } return llist; }