C# 中常用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(); 
} 

        /// <summary>
        /// 使用lambda表达式排除/过滤/清空/删除掉字符串数组中的空字符串
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string[] strArray = { "","111","","222","","333"};           
            Console.WriteLine("输出带有空字符串的数组:");
            foreach (string str in strArray)
            {
                Console.WriteLine(str);
            }
            Console.WriteLine("-------------------------------------------");
            //使用lambda表达式过滤掉空字符串
            strArray = strArray.Where(s=>!string.IsNullOrEmpty(s)).ToArray();
            Console.WriteLine("输出过滤掉空字符串的数组:");
            foreach (string str in strArray)
            {
                Console.WriteLine(str);
            }
            Console.Read();
        }

 

posted @ 2016-10-21 08:17  雅比斯  阅读(552)  评论(0编辑  收藏  举报