基类的参考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
}
}