LinQ是指集成化查询语言,通过映射将数据库内的表名变为C#的类名,将列名作为属性名,将表的关系作为类的成员对象。O--M--RO-Object对象R-Relation关系M-Mapping映射LinQ包括:LinQ to SQL,针对SQL的查询,它是一个带有可视化的操作界面的ORM工具。 LinQ to Object,用来实现对内存中集合对象的查询。 LinQ to DataSet,对强类型化或弱类型化的DataSet或独立的DataTable进行查询。 LinQ to Entity,对实体框架中EDM定义的实体集合进行查询。(一):LinQ to SQL第一步:建立LinQ2SQL类添加一个新建项,选择LinQ to SQL,将用到的表拖进来。第二步:实例化上下文对象,基本所有操作都是通过上下文对象来进行的。MyDBDataContext context = new MyDBDataContext();第三步:操作一、增:1.造对象。 Info data = new Info(); data.Code = "p211"; data.Name = "石头"; data.Sex = false; data.Nation = "N001"; data.Birthday = new DateTime(1990, 1, 2);2.在上下文中注册上面新造的对象。 context.Info.InsertOnSubmit(data);3.提交 context.SubmitChanges();二、删:(由于主、外键关系,需要先删外键,才能删除)1. 找出来 MyDBDataContext context = new MyDBDataContext(); var q = context.Info.Where(p => p.Code == "p003");2. 注册 if (q.Count() > 0) { Info data = q.First(); //注册 context.Work.DeleteAllOnSubmit(data.Work);//删除整个work表 context.Family.DeleteAllOnSubmit(data.Family);//删除fanmily整个表 context.Info.DeleteOnSubmit(data);//最后删除要删除的主键表3. 提交 context.SubmitChanges(); }三、改:1. 找出来 MyDBDataContext context = new MyDBDataContext(); var q = context.Info.Where(p=>p.Code == "p001");2. 改,不用注册 if (q.Count() > 0) { Info data = q.First(); //改 data.Name = "胡生"; data.Nation = "n001"; data.Sex = false; 3. 提交 context.SubmitChanges(); }四、查:1.查所有 MyDBDataContext context = new MyDBDataContext(); //所有人员 var q = context.Info; //var q = from p in context.Info select p; //显示 foreach(Info data in q) { //data.Nation1:当前人员对应的民族对象。 Console.WriteLine(data.Name+"\t"+data.Nation1.Name); //data.Work:当前人员对应的工作记录集合 foreach (Work work in data.Work) { Console.WriteLine("\t"+work.Firm+"\t"+work.Depart); } } 2. 根据主键查询(条件查询),默认返回的都是集合。var q = from p in context.Info where p.Code == "p211" select p;var q = context.Info.Where(p => p.Code == "p211").Where(p => p.Nation1.Name == "汉族"); //Lambda表达式(最最简化的函数,只有参数和函数体的匿名函数)var q = context.Info.Where(p => p.Code == "p211" && p.Nation1.Name=="汉族");var q = context.Info.Where(p => p.Code == "p211"); //默认返回的是集合if (q.Count() > 0) //看看集合中是否查出数据来了{ Info data = q.First(); //取第一个对象出来 Console.WriteLine(data.Nation1.Name + data.Name);}3. 多条件查询var q = from p in context.Car where p.Price > 30 && p.Brand=="b002" select p;var q = context.Car.Where(p => p.Price > 30 && p.Brand == "b002");var q = context.Car.Where(p => p.Price > 30).Where(p => p.Brand == "b002");var q = from p in context.Car where p.Price > 30 || p.Brand == "b002" select p;var q = context.Car.Where(p => p.Price > 30 || p.Brand == "b002");4. 模糊查询 var q = from p in context.Car where p.Name.Contains("5") select p; //包含var q = from p in context.Car where p.Name.StartsWith("奥迪") select p;//开头var q = from p in context.Car where p.Name.EndsWith("奥迪") select p;//结尾var q = context.Car.Where(p => p.Name.Contains("5"));//包含var q = context.Car.Where(p => p.Name.StartsWith("奥迪"));//开头var q = context.Car.Where(p => p.Name.EndsWith("型"));//结尾var q = from p in context.Car where p.Name.Substring(2, 1) == "5" select p;//第三个字符var q = context.Car.Where(p => p.Name.Substring(2,1) == "5");//第三个字符是5 5. Distinct查询var q = (from p in context.Car select p.Brand).Distinct();//去重var q = context.Car.Select(p=>p.Brand).Distinct();//去重6. 连接查询——对象之间的关联关系点出来的。var q = context.Car.Where(p => p.Brand1.Productor.Prod_Name=="一汽丰田");7. 去重查询出显示,IEnumerable是集合的根类型 static void Show(IEnumerableq) { foreach (string d in q) { Console.WriteLine(d); } } 8. 统计查询 MyDBDataContext context = new MyDBDataContext(); //var query = from p in context.Car select p; var query = context.Car;//查出所有 Console.WriteLine(query.Count());//统计个数 Console.WriteLine(query.Max(p => p.Price));//价格的最大值 Console.WriteLine(query.Min(p => p.Price));//价格的最小值 Console.WriteLine(query.Max(p => p.Price * p.Oil));//价格乘以油耗的最大值 Console.WriteLine(query.Sum(p => p.Price));//价格求和 Console.WriteLine(query.Sum(p => p.Price * p.Oil));//价格乘以油耗之后求和 Console.WriteLine(query.Average(p => p.Price));//价格平均值 9. 分页 //分页 MyDBDataContext context = new MyDBDataContext(); var query = context.Car.Skip(2 * 4).Take(4);//每页4条数据,要查第三页的4条数据,跳过8条取下面4条 Show(query);10. 排序 //排序 MyDBDataContext context = new MyDBDataContext(); //var query = context.Car.OrderBy(p=>p.Price); //升序 var query = context.Car.OrderByDescending(p => p.Price);//降序 Show(query);11. 集合操作 //集合操作 MyDBDataContext context = new MyDBDataContext(); var query1 = context.Car.Where(p => p.Brand1.Productor.Prod_Name == "一汽丰田"); var query2 = context.Car.Where(p => p.Price > 30); //交集 var query = query1.Intersect(query2); //并集 //var query = query1.Union(query2); //差集 //var query = query1.Except(query2); //补集 //var query = context.Car.Except(query1); Show(query);