zoukankan      html  css  js  c++  java
  • ADO.net实体类操作

    查询对象

    StudentManageDBEntities2 studentListEntity = new StudentManageDBEntities2();
                var studentReturn = studentListEntity.StudentList.SingleOrDefault(s => s.ID == 1630200001);
                if (studentReturn != null)
                {
                    Console.WriteLine(studentReturn.Name);
                }
                //使用where()方法查询符合条件的数据
                IQueryable<StudentList> query = studentListEntity.StudentList.Where(s => s.Gender == "男");
                foreach (var item in query)
                {
                    Console.WriteLine(item.Name + "  " + item.PhoneNumber);
                }
    

    修改对象

             //想要修改对象先找到对象
            StudentManageDBEntities2 studentEntities = new StudentManageDBEntities2();
            //两种查询方法
            //第一种
            studentEntities.StudentList.FirstOrDefault(s => s.ID == 1630200001);
            //第二种
            var stu = (from s in studentEntities.StudentList
                       where s.ID == 1630200001
                       select s).First<StudentList>();
            Console.WriteLine(stu.Name);
    
    
    删除对象
    
             //删除对象
            studentEntities.StudentList.Remove(stu);
    
    Linq数据投影:(返回一个新的对象)
    
                     var stu = from s in studentEntities.StudentList
                      where s.Gender == "男"
                      //返回的数据映射到一个新的对象stu(stu需要遍历)
                          select new { s.Name, s.StudentAddress };
    
    多表查询
    
            var stuList = from s in studentEntities.StudentList
                          select new
                          {
                           s.Name,
                           s.StudentAddress,
                           s.ClassList.Classid,
                           s.ClassList.ClassName
                          };
    
            foreach (var item in stuList)
            {
                Console.WriteLine(
                item.Name+
                item.StudentAddress+
                item.Classid+
                item.ClassName);
            }
    

    也可以使用JOIN语句

            var stuList = from s in studentEntities.StudentList
                          join c in studentEntities.ClassList on s.Classid equals c.Classid
                          select new
                          {
                             s.Name,s.StudentAddress,s.Classid,s.ClassList.ClassName
                          };
                          
    
            foreach (var item in stuList)
            {
                Console.WriteLine(
                item.Name+
                item.StudentAddress+
                item.Classid+
                item.ClassName);
            }
    

    //也可以进行子查询,如下

            var stuList2 = from s in stuList where s.Classid >= 100 select s;
            foreach (var item in stuList2)
            {
                Console.WriteLine(
                item.Name +
                item.StudentAddress +
                item.Classid +
                item.ClassName);
            }
    
    聚合查询
    
            var stuList = from s in studentEntities.StudentList
                          select new
                          {
                             cunt = (from t in studentEntities.StudentList
                                       select t.ID).Count()
                          };
    
            stuList.ToList();
            Console.WriteLine(stuList.First());
    
    数据关联操作: 创建对象的时候,同时为其选择外键
    
    
             StudentManageDBEntities3 studentEntities = new StudentManageDBEntities3();
            var student = new StudentList()
            {
                Name = "liFlower",
                Gender = "男",
                Birthday = DateTime.Now,
                IDCard = 220153455616355143,
                Age = 15,
                PhoneNumber = "1235656177",
                StudentAddress = "长春",
                //同时操作外键
                Classid = (from c in studentEntities.ClassList where c.ClassName == "英语班" select c.Classid).FirstOrDefault()
            };
    
  • 相关阅读:
    拓扑排序
    最小费用最大流模板
    有上下界的网络流问题
    网络流模板
    LIS+LCS+LCIS
    【Vijos】1218 数字游戏
    【Vijos】1792 摆花
    【Vijos】1431 守望者的逃离
    【wikioi】1029 遍历问题
    背包模版
  • 原文地址:https://www.cnblogs.com/liflower/p/13851848.html
Copyright © 2011-2022 走看看