为了更加深入去理解,所以一开始就没说“如何使用Linq......”。今天只先看了拓展方法和lambda表达式,后续才会慢慢引入Linq,并深入去探究。知其然更知其所以然,是我一贯的学习风格。以下是代码部分。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Collections;
- namespace LinqDemo
- {
- public partial class Form1 : Form
- {
- //定义一个委托
- public delegate string delTransfer(string s);
- public Form1()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 初试牛刀
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnTry_Click(object sender, EventArgs e)
- {
- //找出数组arr中大于50的数,并打印出来
- #region 没有Linq之前的这么做
- int[] arr = {7,9,5,4,18,56,3,45,20,123,98 };
- //ArrayList result = new ArrayList();
- //for (int i = 0; i < arr.Length; i++)
- //{
- // if (arr[i]>50)
- // {
- //将>50的数存放在result动态数组中
- // result.Add(arr[i]);
- // }
- //}
- //遍历该动态数组
- //for (int i = 0; i < result.Count; i++)
- //{
- // Console.WriteLine(result[i]);
- //}
- #endregion
- #region 有了Linq之后这样迭代
- IEnumerable ie = arr.Select(p => p).Where(p => p > 50);
- IEnumerator result = ie.GetEnumerator();
- while (result.MoveNext())
- {
- Console.WriteLine(result.Current);
- }
- #endregion
- }
- /// <summary>
- /// 调用拓展方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnExtraMethod_Click(object sender, EventArgs e)
- {
- string s = "abcdefg";
- Console.WriteLine(s.ToLower());
- Console.WriteLine(s.ToUpper());
- // 调用拓展方法 --》拓展方法是一种特殊的静态方法
- Console.WriteLine(s.toPascal());
- }
- //引入Lambda表达式,Linq中大部分都是Lambda表达式,拓展方法
- private void btnLambda_Click(object sender, EventArgs e)
- {
- // 在.net frameWork 1.0中,使用 委托
- // delTransfer del = new delTransfer(toPascal);//关联并给委托赋值
- //Console.WriteLine(del("abcdefg"));//调用委托,调用相对应的方法
- //在.net framwork 2.0中,使用匿名方法
- //delTransfer trans = delegate(string s) { return s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower(); };
- //Console.WriteLine(trans("abcdefg"));
- //在.net framwork 3.5中,使用Lambda表达式
- delTransfer trans = s => s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
- Console.WriteLine(trans("abcdefg"));
- }
- /// <summary>
- /// 将字符串参数的首字符大写,后面的字母全部小写
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- public string toPascal(string s)
- {
- return s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
- }
- /// <summary>
- /// 体验Linq中的拓展方法--Select
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSelect_Click(object sender, EventArgs e)
- {
- List<string> list = new List<string>();
- list.Add("丁浪");
- list.Add("艳刘");
- list.Add("王五");
- list.Add("李四");
- list.Add("张三");
- list.Add("王兴魁");
- //var reslut = list.Select(p=>p);//查询出所有对象
- Console.WriteLine(double.MaxValue);
- //查出以“丁”开头的对象并打印
- //var reslut = list.Select(p=>p).Where(p => p.StartsWith("丁"));
- int a = 0;
- string s = a >= 0 ? "a是正整数" : "a是负数";
- Console.WriteLine(s);
- //按姓氏排序
- //var reslut = list.Select(p=>p).OrderBy(p =>p.Substring(0,1));
- //reslut.print();
- //按“姓氏”分组
- var reslut = list.Select(p => p).GroupBy(p => p.Substring(0, 1));
- foreach (var item in reslut)
- {
- Console.WriteLine("/n-------------------------------/n");
- Console.WriteLine("本组人都是姓"+"/'"+item.Key+"/'"+"的");
- foreach (var name in item)
- {
- Console.WriteLine("姓"+"/'"+item.Key+"/'"+"的有:"+name);
- }
- Console.WriteLine("/n-------------------------------/n");
- }
- }
- }
- /// <summary>
- /// 拓展类,拓展方法
- /// </summary>
- public static class ExtraClass
- {
- // 加this关键字,表明为该类型(string)添加了拓展方法
- public static string toPascal(this string str)
- {
- //将字符串参数的首字符大写,后面的字母全部小写
- return str.Substring(0, 1).ToUpper() + str.Substring(1).ToLower();
- }
- public static void print(this IEnumerable<string> ie)
- {
- IEnumerator<string> result = ie.GetEnumerator();
- while (result.MoveNext())
- {
- Console.WriteLine("/n-------------------------------/n");
- Console.WriteLine(result.Current);
- Console.WriteLine("/n-------------------------------/n");
- }
- }
- }
- }