using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{class Program{static void Main(string[] args){//创建数据库实体ModelStudentContainer DemoDB = new ModelStudentContainer();#region 增加数据创建具体实体(表中一行)//Student stu1 = new Student()//{// ID = 1,// Name = "张飞",// Age = 20//};//Student stu2 = new Student()//{// ID = 2,// Name = "关羽",// Age = 21//};//Student stu3 = new Student()//{// ID = 3,// Name = "赵云",// Age = 22//};//Student stu4 = new Student()//{// ID = 4,// Name = "刘备",// Age = 23//};//Student stu5 = new Student()//{// ID = 5,// Name = "刘表",// Age = 24//};//Student stu6 = new Student()//{// ID = 6,// Name = "刘禅",// Age = 25//};将实体stu添加到数据库实体对象中//DemoDB.Student.AddObject(stu1);//DemoDB.Student.AddObject(stu2);//DemoDB.Student.AddObject(stu3);//DemoDB.Student.AddObject(stu4);//DemoDB.Student.AddObject(stu5);//DemoDB.Student.AddObject(stu6);保存对数据库的修改//DemoDB.SaveChanges();#endregion#region 查询1:一般查询,查询所有数据//var result = from u in DemoDB.Student// select u;//foreach (var item in result)//{// Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);//}#endregion#region 查询2:使用ToList防止延迟加载,拆分子查询这个查询结果可以保存到服务器内存中//var result1 = (from u in DemoDB.Student// where u.Age >= 22// select u).ToList();//var result2 = from u in result1// where u.Name != "赵云"// select u;//foreach (var item in result2)//{// Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);//}#endregion#region 查询3:使用Where方法(lambda表达式)使用了Where泛型方法,要传入一个lambda表达式//var result = DemoDB.Student// .Where (t => t.ID == 1);//foreach (var item in result)//{// Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);//}#endregion#region 查询4:Linq分页查询要跳过多少页//int skipPage;每页两条数据//int countPerPage=2;从跳过0页到跳过2页(输出第1-3页)//for (skipPage = 0; skipPage <= 2; skipPage++)//{// var result = DemoDB.Student分页必须排序// .OrderBy(t => t.ID)跳过指定页数// .Skip (skipPage * countPerPage)获取条数// .Take (countPerPage);// Console.WriteLine("第{0}页:", skipPage + 1);输出查询结果(该页)// foreach (var item in result)// {// Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);// }// Console.WriteLine();//}#endregion#region 查询5:使用匿名类查询多个字段//var result = from p in DemoDB.Student// where p.ID < 4定义一个包含了ID和Name信息的匿名类// select new { p.ID, p.Name };//foreach (var item in result)//{这里无法输出年龄// Console.WriteLine("ID:{0},姓名:{1}。", item.ID, item.Name);//}#endregion#region 修改1:修改1条记录这里没有写where字句,会查询出4条记录//var result = from u in DemoDB.Student// select u;但是这个方法一次只能取出一条记录注:First方法如果返回空值则抛异常//var target = result.FirstOrDefault ();所以只修改了查询结果集中的第一条记录//target.Name = "司马懿";//DemoDB.SaveChanges();#endregion#region 修改2:修改多条记录//var result = from u in DemoDB.Student// select u;使用循环方式,修改所有查询到的记录//foreach (var item in result)//{// item.Age = 10;//}//DemoDB.SaveChanges();#endregion#region 删除//var result = from u in DemoDB.Student// where u.Name == "关羽"// select u;删除所有查询结果(这里只有一条结果)//foreach (var item in result)//{// DemoDB.Student.DeleteObject(item);//}//DemoDB.SaveChanges();#endregionConsole.WriteLine("执行完毕!");Console.ReadKey();}}}