日期:2014-05-20 浏览次数:20705 次
private void button1_Click(object sender, EventArgs e) { //定义一个整形数组 int [] arr = { 123, 1223, 45, 67, 1, 23, 23, 45, 67, 78 }; //找出数组中小于100的数 #region 没有Linq之前我们这样做 //ArrayList arrList = new ArrayList(); //for (int i = 0; i < arr.Length; i++) //{ // if (arr[i] < 100) // { // arrList.Add(arr[i]); // } //} // //输了arrList元素 //foreach (int i in arrList) //{ // Console.WriteLine(i); //} #endregion #region 有了Linq可以这样做 var Result = arr.Select(p => p).Where(p => p < 100); IEnumerator<int> IResult = Result.GetEnumerator(); while (IResult.MoveNext()) { Console.WriteLine(IResult.Current); } #endregion }
private void button2_Click(object sender, EventArgs e) { List<string> list = new List<string>(); list.Add("zhang san"); list.Add("zhang san feng"); list.Add("li xiao xiao"); list.Add("wang wu"); list.Add("song guo jun"); list.Add("song jun jun"); //Linq to object,lambda表达式 // var Result = list.Select(p => p); //Linq排序 //var Result = list.OrderBy(p => p); //按条件输入 var Result = list.Where(p => p.StartsWith("song")).Select(p => p); //使用拓展方法,输入泛型集合中的元素 Result.Print(); //普通的访问 //foreach (string s in list) //{ // Console.WriteLine(s); //} } //静态类 public static class ExpandClss { //静态类中的拓展方法 //为IEnumerable<string>接口类型对象定义方法,实现元素的输出 public static void Print(this IEnumerable<string> a) { IEnumerator<string> result = a.GetEnumerator(); while (result.MoveNext()) { Console.WriteLine(result.Current); } } //为IEnumerable<int>接口类型对象定义方法,实现元素的输出 public static void Print(this IEnumerable<int> a) { IEnumerator<int> result = a.GetEnumerator(); while (result.MoveNext()) { Console.WriteLine(result.Current); } } } private void button3_Click_1(object sender, EventArgs e) { int[] arr = {80, 12, 13, 24, 345, 546, 77, 89, 100, 23,9 }; //求数组中的最大数 Console.WriteLine(arr.Max()); //求数组中的最小数 Console.WriteLine(arr.Min()); //求数组中的平均数 Console.WriteLine(arr.Average()); //求数组中元素的个数 Console.WriteLine(arr.Count()); Console.WriteLine(arr.Length); Console.WriteLine("--------------------------------"); //排序类 //将第一个数字转换成字母,然后按照字母排序 // var Result = arr.OrderBy(p => p.ToString().Substring(0, 1)); //二次排序 //var Result = arr.OrderBy(p => p.ToString().Substring(0, 1)).ThenBy(p => p);//2次 //分区类 //var Result = arr.Skip(3).Take(4); //var Result = arr.SkipWhile(p => p > 11);//跳过满足条件的元素,然后返回剩余元素。 //var Result = arr.TakeWhile(p => p > 12); //生成类--类中的静态方法 //var Result = System.Linq.Enumerable.Range(10, 10); var Result = System.Linq.Enumerable.Repeat("abc", 10); Result.Print(); } }
private void btnLanguage_Click(object sender, EventArgs e) { //Linq查询语句,LinQ TO Object //1.这里定义查询数据源 List<string> list = new List<string>(); list.Add("zhang san"); list.Add("song jun jun"); list.Add("zhang san feng"); list.Add("li xiao xiao"); list.Add("wang wu"); list.Add("song guo jun"); //输出list中的所有元素 //var Result = from p in list select p; //输出姓song的元素, 语句和方法的混编模式 // var Result = (from p in list select p).Where(p => p.StartsWith("song")); //2.这里创建查询 //排序 var Result = from p in list orderby p select p; //3.执行查询,此处是调用了一个扩展方法,也可以使用foreach来遍历 Result.Print(); } public static class ExpandClss { //静态类中的拓展方法 public static void Print(this IEnumerable<string> a) { IEnumerator<string> result = a.GetEnumerator(); while (result.MoveNext()) { Console.WriteLine(result.Current); } } public static void Print(this IEnumerable<int> a) { IEnumerator<int> result = a.GetEnumerator(); while (result.MoveNext()) { Console.WriteLine(result.Current); } } }
private int OutputSum() { int[] numbers=new int[]{1,2,3,4,5,6,7,8,9,10} var evenNumQuery = from num in numbers where (num % 2) == 0 select num; int evenNumCount = evenNumQuery.Count(); }
private IList<T> Test() { List<int> numQuery2 = (from num in numbers where (num % 2) == 0 select num).ToList(); // or like this: // numQuery3 is still an int[] } private Array Test1() { var numQuery3 = (from num in numbers where (num % 2) == 0 select num).ToArray() }