LINQ操作数组(交集,并集,差集,最值,平均,去重复)
static void Main(string[] args) { int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = { 4, 5, 6, 7, 8, 9, 10 }; int[] c = { 1, 2, 3, 3, 4, 1, 2, 4, 6, 1, 6, 5 }; // 交集 var fuck = a.Intersect(b); // 并集 var shit = a.Union(b); // a有b没有的 var diff1 = a.Except(b); // b有a没有的 var diff2 = b.Except(a); var max = a.Max(); var min = a.Min(); var avg = a.Average(); var dis = c.Distinct(); Print(fuck); Print(shit); Print(diff1); Print(diff2); Console.WriteLine(max); Console.WriteLine(min); Console.WriteLine(avg); Print(dis); Console.ReadKey(); } private static void Print(IEnumerable<int> list) { foreach (var item in list) { Console.Write(item + " "); } Console.WriteLine(); }
my-code
DateTime dt1 = DateTime.Now; string filePath = "c:\\100w.txt"; string filePathBlack = "c:\\100.txt"; var mobiles = Reader(filePath); var black = Reader(filePathBlack); var mobileToDis = mobiles.ToList().Distinct();//去重 var mobileToBlack = mobileToDis.Except(black);//去黑名单 //去不规则手机号 var newMobiles = System.Linq.Enumerable.Where(mobileToBlack, n => n.ToString().Length >= 11); int count = newMobiles.Count(); IDictionary<string, string> dic = new Dictionary<string, string>(); foreach (var item in newMobiles) { dic.Add(item, item); } var dt2 = DateTime.Now; Console.Write((dt2-dt1).TotalMilliseconds); //Wirte(mobiles); Console.Read();
一些参考:
http://msdn.microsoft.com/zh-cn/library/bb397894.aspx
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect.aspx
出处:http://diaosbook.com/