LINQ小结
LINQ是自.net 3.5出现的新东东,个人认为它主要是对数据进行转换和对集合的查询
废话不多说了,直接看代码,代码里有相应的解释,在这里不作过多的说明
protected void Page_Load(object sender, EventArgs e) { List<Student> students = new List<Student>(); students.Add(new Student("11", "kingtiger")); students.Add(new Student("2", "wangbiao")); students.Add(new Student("3", "Mr Wang")); //var arr = students.Cast<Student>().OrderBy(r => r.ID.PadLeft(2, '0'));//按照ID进行排序 //var arr = students.OfType<Student>().OrderBy(r => r.Name.Length);//排序应用,降序的话在前面加上- var arr = students.Cast<Student>().Where(r => r.ID == "01");//进行查询 foreach (var l in arr) { Response.Write(l.ID + "#" + l.Name + "<br/>"); } string[] strings = { "kingtiger", "wangbiao", null, "Mr wang" }; //IEnumerable<string> ieStrings = strings.Cast<string>().Where(s => s.Length == 3);//该方法会报错,Cast会对所有选项进行转换,转换失败则抛出异常 IEnumerable<string> ieStrings = strings.Cast<string>().Where(s => s.Length == 3);//该方法只转换能进行转换的选项,不能转换则跳出 foreach (string s in ieStrings) { Response.Write(s + "<br/>"); } }