用Linq To SQL 搭建底层

用Linq To SQL 搭建底层

接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace Project.DAL
{
    interface IBaseService where T:class,new()
    {
        IQueryable QueryAll(params Expression>[] where);
        IQueryable QueryAll(Expression> order, bool isAsc = true, params Expression>[] where);
        IQueryable QueryAll(out int total, int skip = 0, int take = 10, Expression> order = null, bool isAsc = true, params Expression>[] where);
        void Insert(T t);
        void Insert(IEnumerable t);
        void Delete(T t);
        void Delete(IEnumerable t);
        bool SaveChange();
    }
}

底层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace Project.DAL
{
    public class BaseService : IBaseService where T : class, new()
    {
    //上下文对象
    Models.QuestionDBDataContext dbContext = new Models.QuestionDBDataContext();//直接复制会报错应先在Models里引入数据库

    private IQueryable table = null;
    private IQueryable GetTable()
    {
        if (table == null)
        {
            table = (IQueryable)dbContext.GetTable();
        }
        return table;
    }

    /// 
    /// 查询
    /// 
    /// 查询条件
    /// 
    public IQueryable QueryAll(params Expression>[] where)
    {
        IQueryable iq = GetTable();
        if (where != null || where.Length < 1)
        {
            foreach (var item in where)
            {
                iq = iq.Where(item);
            }
        }
        return iq;
    }

    /// 
    /// 排序
    /// 
    /// 排序类型
    /// 排序列
    /// 升序or降序
    /// 
    /// 
    public IQueryable QueryAll(Expression> order, bool isAsc = true, params Expression>[] where)
    {
        var iq = QueryAll(where);
        if (isAsc)
        {
            iq = iq.OrderBy(order);
        }
        else
        {
            iq = iq.OrderByDescending(order);
        }
        return iq;
    }

    /// 
    /// 分页查询
    /// 
    /// 
    /// 总数据数
    /// 跳过n条
    /// 取n条(一页显示几条)
    /// 
    /// 
    /// 
    /// 
    public IQueryable QueryAll(out int total, int skip = 0, int take = 10, Expression> order = null, bool isAsc = true, params Expression>[] where)
    {
        var iq = QueryAll(order, isAsc, where);
        total = iq.Count();
        return iq.Skip(skip).Take(take);
    }

    public void Insert(T t)
    {
        dbContext.GetTable().InsertOnSubmit(t);
    }

    public void Insert(IEnumerable t)
    {
        dbContext.GetTable().InsertAllOnSubmit(t);
    }

    public void Delete(T t)
    {
        dbContext.GetTable().DeleteOnSubmit(t);
    }

    public void Delete(IEnumerable t)
    {
        dbContext.GetTable().DeleteAllOnSubmit(t);
    }

    /// 
    /// 保存修改
    /// 
    /// 
    public bool SaveChange()
    {
        try
        {
            dbContext.SubmitChanges();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
}

}

你可能感兴趣的:(用Linq To SQL 搭建底层)