其中

通用类名:DBhelper

实体类:UserInfo

 

复制代码
 1   //新增
 2   DBHelper<UserInfo> dbhelper = new DBHelper<UserInfo>();
 3   UserInfo userinfo = new UserInfo();
 4   userinfo.Name = "1";
 5   dbhelper.Add(userinfo);
 6 
 7   //根据条件查找列表
 8   var entityes = dbhelper.FindList(x => x.Name == "1");
 9   List<UserInfo> list = entityes.ToList<UserInfo>();
10 
11   Response.Write("姓名为1的数量为:"+list.Count);
12 
13   //分页查找
14   int rowcount = 0;
15   var entityes2 = dbhelper.FindPagedList(2, 10, out rowcount,x=>x.Name=="1",s=>s.Id,true);
复制代码

2.EF(Entity Framework)通用DBHelper通用类,增删改差以及列表

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

public class DBHelper<T> where T : class
{
    AproSysEntities context = new AproSysEntities();
    /// <summary>
    /// 新增一个实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Add(T entity)
    {
        context.Entry<T>(entity).State = System.Data.EntityState.Added;
        return context.SaveChanges();
    }
    /// <summary>
    /// 删除一个实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Remove(T entity)
    {
        context.Entry<T>(entity).State = System.Data.EntityState.Deleted;
        return context.SaveChanges();
    }
    /// <summary>
    /// 修改一个实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Update(T entity)
    {
        context.Entry<T>(entity).State = System.Data.EntityState.Modified;
        return context.SaveChanges();
    }
    /// <summary>
    /// 批量新增实体
    /// </summary>
    /// <param name="dbContext"></param>
    /// <returns></returns>
    public int AddList(params T[] entities)
    {
        int result = 0;
        for (int i = 0; i < entities.Count(); i++)
        {
            if (entities[i] == null)
                continue;
            context.Entry<T>(entities[i]).State = System.Data.EntityState.Added;
            if (i != 0 && i % 20 == 0)
            {
                result += context.SaveChanges();
            }
        }
        if (entities.Count() > 0)
            result += context.SaveChanges();
        return result;
    }
    /// <summary>
    /// 批量删除实体
    /// </summary>
    /// <param name="where"></param>
    /// <returns></returns>
    public int RemoveList(Expression<Func<T, bool>> where)
    {
        var temp = context.Set<T>().Where(where);
        foreach (var item in temp)
        {
            context.Entry<T>(item).State = System.Data.EntityState.Deleted;
        }
        return context.SaveChanges();
    }
    /// <summary>
    /// 按条件查询
    /// </summary>
    /// <param name="where"></param>
    /// <returns></returns>
    public IQueryable<T> FindList(Expression<Func<T, bool>> where)
    {
        var temp = context.Set<T>().Where(where);
        return temp;
    }
    /// <summary>
    /// 按条件查询,排序
    /// </summary>
    /// <typeparam name="S"><peparam>
    /// <param name="where"></param>
    /// <param name="orderBy"></param>
    /// <param name="isAsc"></param>
    /// <returns></returns>
    public IQueryable<T> FindList<S>(Expression<Func<T, bool>> where, Expression<Func<T, S>> orderBy, bool isAsc)
    {

        var list = context.Set<T>().Where(where);
        if (isAsc)
            list = list.OrderBy<T, S>(orderBy);
        else
            list = list.OrderByDescending<T, S>(orderBy);
        return list;
    }
    /// <summary>
    /// 按条件查询,分页
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="rowCount"></param>
    /// <param name="where"></param>
    /// <returns></returns>
    public IQueryable<T> FindPagedList(int pageIndex, int pageSize, out int rowCount, Expression<Func<T, bool>> where)
    {
        var list = context.Set<T>().Where(where);
        rowCount = list.Count();
        list = list.Skip(pageSize * (pageIndex - 1)).Take(pageSize);
        return list;
    }
    /// <summary>
    /// 按条件查询,分页,排序
    /// </summary>
    /// <typeparam name="S"><peparam>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="rowCount"></param>
    /// <param name="where"></param>
    /// <param name="orderBy"></param>
    /// <param name="isAsc"></param>
    /// <returns></returns>
    public IQueryable<T> FindPagedList<S>(int pageIndex, int pageSize, out int rowCount, Expression<Func<T, bool>> where, Expression<Func<T, S>> orderBy, bool isAsc)
    {
        var list = context.Set<T>().Where(where);
        rowCount = list.Count();
        if (isAsc)
            list = list.OrderBy<T, S>(orderBy).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
        else
            list = list.OrderByDescending<T, S>(orderBy).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
        return list;
    }
}