• Entity Framework 学习第二天 续


    今天来写一点不一样的删除,修改,查询

    下面只写了几个方法

     1         /// <summary>
     2         /// 根据删除条件进行删除
     3         /// </summary>
     4         /// <param name="removeWhere"></param>
     5         public void remove(System.Linq.Expressions.Expression<Func<StudentInfo, bool>> removeWhere)
     6         {
     7             //使用查找方式,现将要删除的条件传递进来,将要删除的数据查询出来
     8             List<StudentInfo> studentList = db.Student.Where(removeWhere).ToList();
     9             //遍历查询出来的数据,然后进行删除操作,这里用到lambda表达式,后期有时间会写一下有关的使用
    10             studentList.ForEach(s => db.Student.Remove(s));
    11             db.SaveChanges();
    12         }
    13         /// <summary>
    14         /// 修改数据
    15         /// </summary>
    16         /// <param name="student"></param>
    17         /// <param name="strs">可变参数,传递那些属性被修改了</param>
    18         public void update(StudentInfo student,params string[] strs)
    19         {
    20             DbEntityEntry entry= db.Entry<StudentInfo>(student);
    21             entry.State = System.Data.EntityState.Unchanged;
    22             //遍历属性,修改其标志位
    23             foreach (string temp in strs)
    24             {
    25                 entry.Property(temp).IsModified = true;
    26             }
    27             db.SaveChanges();
    28         }
    29 
    30         /// <summary>
    31         /// 基本查询
    32         /// </summary>
    33         /// <param name="where"></param>
    34         /// <returns></returns>
    35         public List<StudentInfo> where(System.Linq.Expressions.Expression<Func<StudentInfo,bool>> where)
    36         {
    37            return db.Student.Where(where).ToList();
    38         }
    39         /// <summary>
    40         /// 带排序条件的查询,并包括页数,页容量
    41         /// </summary>
    42         /// <typeparam name="TKey"></typeparam>
    43         /// <param name="where"></param>
    44         /// <param name="orderBy"></param>
    45         /// <param name="pageSize"></param>
    46         /// <param name="pageIndex"></param>
    47         /// <returns></returns>
    48         public List<StudentInfo> whereByOrder<TKey>(Expression<Func<StudentInfo, bool>> where, Expression<Func<StudentInfo, TKey>> orderBy, int pageSize, int pageIndex)
    49         {
    50             return db.Student.Where(where).OrderBy(orderBy).Skip(pageIndex - 1).Take(pageSize).ToList();
    51         }
    View Code

    看上去比第一天的内容上了些档次呢。哈哈

  • 相关阅读:
    陈欧代言
    location传值
    jsp中button传值
    电影
    排序
    比较两个字符,相等输出yes,不相等输出no
    查表求平方值
    数据库查询调优(SQL 2008)
    HelloWorld
    关于缓存 (这个自己也在慢慢学习,慢慢总结中,有路过的,求指点,赶紧不尽。。。)
  • 原文地址:https://www.cnblogs.com/ljs0322/p/3791543.html
Copyright © 2020-2023  润新知