LINQ(Language Intergrated Query):集成查询语言
string[] array = { "1", "5", "6", "7" }; var query = from s in array select s; foreach(var aaa in query) { Console.WriteLine(aaa); }
DataTable dt = new DataTable(); dt.Columns.Add("name"); dt.Columns.Add("sex"); dt.Columns.Add("age"); DataRow row = dt.NewRow(); row["name"] = "张三"; row["sex"] = "男"; row["age"] = "20"; dt.Rows.Add(row); DataRow row1 = dt.NewRow(); row1["name"] = "李四"; row1["sex"] = "女"; row1["age"] = "18"; dt.Rows.Add(row1); //LINQ 查询适用于实现的数据源 IEnumerable<T> 接口或System.Query.IQueryable接口。 //DataTable类默认是没有实现以上接口的。 //所以要在DataTable中使用LINQ查询,需要调用一下AsEnumerable方法,返回一个EnumerableRowCollection<DataRow> 集合。 var person = dt.AsEnumerable();// var results = person.OrderBy(x => x.Field<string>("age"));//以年龄排序 lambda foreach (DataRow ro in results) { MessageBox.Show(ro["name"].ToString() + ro["sex"] + ro["age"]); }
private static DataTable GetStuTable() { int[] id = { 4, 5, 1, 3, 2, 7, 6 }; string[] name = {"Tom","Jack","HelloWorld","Visual Studio","Gril","Timmy","Geo" }; DataTable table = new DataTable("Student"); table.Columns.Add("ID", typeof(int)); table.Columns.Add("Name", typeof(string)); for(int i = 0;i< id.Length;i++) { table.Rows.Add(new object[] { id[i],name[i]});//加数组 } return table; }