zoukankan      html  css  js  c++  java
  • 基类的参考Expression能传一个lambda表达式

    using System;
    using System.Collections.Generic;
    using System.Data.Entity.Infrastructure;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Text;

    namespace P04DAL
    {
    /// <summary>
    /// 用户表 操作类
    /// </summary>
    public class User
    {
    /// <summary>
    /// 数据上下文对象
    /// </summary>
    P05MODEL.LeaveWordBoradEntities db = new P05MODEL.LeaveWordBoradEntities();

    //public User()
    //{
    // //Predicate<P05MODEL.User> a = delegate(P05MODEL.User u) { return u.uId == 1; };

    // //this.DelBy(ab => ab.uId == 1);

    // //P05MODEL.User u = new P05MODEL.User() { uId = 1, uLoginName = "asdfasdf" };
    // //this.Modify(u, "uLoginName");

    // //List<P05MODEL.User> list = this.GetListBy(u2 => u2.uId > 1 && u2.uIsDel == false);

    // //List<P05MODEL.User> listSorted = this.GetListBy<int>(u3 => u3.uId > 1, u4 => u4.uId);
    //}

    #region 1.0 新增 实体 +int Add(P05MODEL.User model)
    /// <summary>
    /// 新增 实体
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public int Add(P05MODEL.User model)
    {
    db.Users.Add(model);
    return db.SaveChanges();//保存成功后,会将自增的id设置给 model的 主键属性,并返回受影响行数
    }
    #endregion

    #region 2。0 根据 用户 id 删除 +int Del(int uId)
    /// <summary>
    /// 根据 用户 id 删除
    /// </summary>
    /// <param name="uId"></param>
    /// <returns></returns>
    public int Del(int uId)
    {
    P05MODEL.User usr = new P05MODEL.User() { uId = uId };
    db.Users.Attach(usr);
    db.Users.Remove(usr);
    return db.SaveChanges();
    }
    #endregion

    #region 3.0 根据条件删除 +int DelBy(Expression<Func<P05MODEL.User, bool>> delWhere)
    /// <summary>
    /// 3.0 根据条件删除
    /// </summary>
    /// <param name="delWhere"></param>
    /// <returns></returns>
    public int DelBy(Expression<Func<P05MODEL.User, bool>> delWhere)
    {
    List<P05MODEL.User> listDeleting = db.Users.Where(delWhere).ToList();
    listDeleting.ForEach(u => db.Users.Remove(u));
    return db.SaveChanges();
    }
    #endregion

    #region 4.0 修改 +int Modify(P05MODEL.User model, params string[] proNames)
    /// <summary>
    /// 4.0 修改,如:
    /// P05MODEL.User u = new P05MODEL.User() { uId = 1, uLoginName = "asdfasdf" };
    /// this.Modify(u, "uLoginName");
    /// </summary>
    /// <param name="model">要修改的实体对象</param>
    /// <param name="proNames">要修改的 属性 名称</param>
    /// <returns></returns>
    public int Modify(P05MODEL.User model, params string[] proNames)
    {
    DbEntityEntry entry = db.Entry<P05MODEL.User>(model);
    entry.State = System.Data.EntityState.Unchanged;
    foreach (string proName in proNames)
    {
    entry.Property(proName).IsModified = true;
    }
    return db.SaveChanges();
    }
    #endregion

    #region 5.0 根据条件查询 +List<P05MODEL.User> GetListBy(Expression<Func<P05MODEL.User,bool>> whereLambda)
    /// <summary>
    /// 5.0 根据条件查询 +List<P05MODEL.User> GetListBy(Expression<Func<P05MODEL.User,bool>> whereLambda)
    /// </summary>
    /// <param name="whereLambda"></param>
    /// <returns></returns>
    public List<P05MODEL.User> GetListBy(Expression<Func<P05MODEL.User, bool>> whereLambda)
    {

    return db.Users.Where(whereLambda).ToList();

    }
    #endregion


    #region 5.1 根据条件 排序 和查询 + List<P05MODEL.User> GetListBy<TKey>
    /// <summary>
    /// 5.1 根据条件 排序 和查询
    /// </summary>
    /// <typeparam name="TKey">排序字段类型</typeparam>
    /// <param name="whereLambda">查询条件 lambda表达式</param>
    /// <param name="orderLambda">排序条件 lambda表达式</param>
    /// <returns></returns>
    public List<P05MODEL.User> GetListBy<TKey>(Expression<Func<P05MODEL.User, bool>> whereLambda, Expression<Func<P05MODEL.User, TKey>> orderLambda)
    {
    return db.Users.Where(whereLambda).OrderBy(orderLambda).ToList();
    }
    #endregion

    #region 6.0 分页查询 + List<P05MODEL.User> GetPagedList<TKey>
    /// <summary>
    /// 6.0 分页查询 + List<P05MODEL.User> GetPagedList<TKey>
    /// </summary>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">页容量</param>
    /// <param name="whereLambda">条件 lambda表达式</param>
    /// <param name="orderBy">排序 lambda表达式</param>
    /// <returns></returns>
    public List<P05MODEL.User> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<P05MODEL.User, bool>> whereLambda, Expression<Func<P05MODEL.User, TKey>> orderBy)
    {
    return db.Users.Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
    }
    #endregion
    }

    }

  • 相关阅读:
    年终总结 2016-08-28 22:04 422人阅读 评论(26) 收藏
    [mysql]MySQL Daemon failed to start 2016-08-14 21:27 1121人阅读 评论(18) 收藏
    solrr初步了解 2016-07-31 22:29 380人阅读 评论(4) 收藏
    基于spring-boot的测试桩设计--几种常见的controller
    利用Factory-boy和sqlalchemy来批量生成数据库表数据
    job中shell脚本异常(删除不存在容器),导致job被打断执行的问题 脚本优化方法
    利用Factory-boy来生成实例数据
    pytest相关资源收集
    pytest 用 @pytest.mark.usefixtures("fixtureName")装饰类,可以让执行每个case前,都执行一遍指定的fixture
    pytest fixture 利用 params参数实现用例集合
  • 原文地址:https://www.cnblogs.com/sumg/p/3743998.html
Copyright © 2011-2022 走看看