/// <summary>
/// 查询业务基实现
/// </summary>
/// <typeparam name="T"></typeparam>
public class QueryBaseBusiness<T> : IQueryBaseBusiness<T> where T : class
{
/// <summary>
/// 是否跟踪上下文
/// <para>可提升性能</para>
/// </summary>
public bool asNoTracking { get; set; } /// <summary>
/// 数据上下文对象
/// </summary>
public Payu_DatabaseContext context { get; set; } /// <summary>
/// Initializes a new instance of the <see cref="Payu.Business.Base.QueryBaseBusiness{T}"/> class.
/// </summary>
public QueryBaseBusiness()
{
context = EFContextFactory.GetCurrentDbContext();
} /// <summary>
/// 设置query 查询对象
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public IQueryable<T> SetQueryable(IQueryable<T> query)
{
if (!this.asNoTracking)
{
query = query.AsNoTracking();
} return query;
} /// <summary>
/// 设置query 查询对象
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public IQueryable<OutT> SetQueryable<OutT>(IQueryable<OutT> query) where OutT : class
{
if (!this.asNoTracking)
{
query = query.AsNoTracking();
} return query;
} /// <summary>
/// 根据表达式查询返回总个数.
/// </summary>
/// <param name="where">The where.</param>
/// <returns></returns>
public IBusinessResponse<Int32> GetCount(Expression<Func<T, bool>> where)
{
var count = this.GetQueryable(where).Count(); return new BusinessResponse<Int32>(count);
} /// <summary>
/// 根据表达式查询返回总个数
/// </summary>
/// <param name="where"></param>
/// <param name="selectExpression">统计的对象</param>
/// <returns></returns>
public IBusinessResponse<Int32> GetCount<TKey>(Expression<Func<T, bool>> where, Expression<Func<T, TKey>> selectExpression)
{
var query = this.GetQueryable(where); var count = query.Select(selectExpression).Count(); return new BusinessResponse<Int32>(count);
} /// <summary>
/// 根据 where 返回集合对象
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public IBusinessResponse<ICollection<T>> GetByWhere(Expression<Func<T, bool>> where)
{
var result = this.GetQueryable(where).ToList(); return new BusinessResponse<ICollection<T>>(result);
} /// <summary>
/// 根据 where 条件返回 集合对象
/// </summary>
/// <typeparam name="TOut">返回类型</typeparam>
/// <param name="where">查询条件.</param>
/// <param name="selectExpression">查询表达式</param>
/// <returns></returns>
public IBusinessResponse<ICollection<TOut>> GetByWhere<TOut>(Expression<Func<T, bool>> where, Expression<Func<T, TOut>> selectExpression)
{
var query_data = this.GetQueryable().Where(where).Select(selectExpression).ToList(); return new BusinessResponse<ICollection<TOut>>(query_data);
} /// <summary>
/// 根据 where 条件返回 集合对象
/// </summary>
/// <typeparam name="TOut">返回的泛型类型</typeparam>
/// <typeparam name="TKey">排序的类型</typeparam>
/// <param name="where">查询条件.</param>
/// <param name="orderLambda">排序表达式</param>
/// <param name="selectExpression">查询表达式</param>
/// <returns></returns>
public IBusinessResponse<ICollection<TOut>> GetByWhere<TOut, TKey>(Expression<Func<T, bool>> where, Expression<Func<T, TKey>> orderLambda, Expression<Func<T, TOut>> selectExpression)
{
var query_data = this.GetQueryable().Where(where).OrderBy(orderLambda).Select(selectExpression).ToList(); return new BusinessResponse<ICollection<TOut>>(query_data);
} /// <summary>
/// 根据表达式返回单个元素
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public IBusinessResponse<T> GetFirstByWhere(Expression<Func<T, bool>> where)
{
var entity = this.GetQueryable(where).FirstOrDefault(); if (entity == null)
{
return new BusinessResponse<T>(false, "数据不存在", "Not_Data");
} return new BusinessResponse<T>(entity);
} /// <summary>
/// 根据表达式返回单个元素
/// <para>带有Select</para>
/// </summary>
/// <param name="where"></param>
/// <param name="selectExpression">查询对象</param>
/// <returns></returns>
public IBusinessResponse<TOut> GetFirstByWhere<TOut>(Expression<Func<T, bool>> where, Expression<Func<T, TOut>> selectExpression)
{
var query = this.GetQueryable(where); var entity = query.Select(selectExpression).FirstOrDefault(); return new BusinessResponse<TOut>(entity);
} /// <summary>
/// 获得唯一一个对象
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public IBusinessResponse<T> GetSingleByWhere(Expression<Func<T, bool>> where)
{
var entity = context.Set<T>().Where(where).SingleOrDefault(); if (entity == null)
{
return new BusinessResponse<T>(false, "数据不存在", "Not_Data");
}
return new BusinessResponse<T>(entity);
} /// <summary>
/// 获得唯一一个对象
/// </summary>
/// <param name="where"></param>
/// <param name="selectExpression"></param>
/// <returns></returns>
public IBusinessResponse<TOut> GetSingleByWhere<TOut>(Expression<Func<T, bool>> where, Expression<Func<T, TOut>> selectExpression)
{
var entity = this.GetQueryable(where).Select(selectExpression).SingleOrDefault(); return new BusinessResponse<TOut>(entity);
} /// <summary>
/// 根据表达式返回最后一个元素
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public IBusinessResponse<T> GetLastByWhere(Expression<Func<T, bool>> where)
{
var query = context.Set<T>().Where(where).AsQueryable(); var entity = query.LastOrDefault(); if (entity == null)
{
return new BusinessResponse<T>(false, "数据不存在", "Not_Data");
} return new BusinessResponse<T>(entity);
} /// <summary>
/// 根据表达式返回最后一个元素
/// </summary>
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <param name="where">The where.</param>
/// <param name="selectExpression">The select expression.</param>
/// <returns></returns>
public IBusinessResponse<TOut> GetLastByWhere<TOut>(Expression<Func<T, bool>> where, Expression<Func<T, TOut>> selectExpression)
{
var query = this.GetQueryable(where); var entity = query.Select(selectExpression).LastOrDefault(); return new BusinessResponse<TOut>(entity);
} /// <summary>
///
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public IBusinessResponse<bool> AnyByWhere(Expression<Func<T, bool>> where)
{
var isAny = this.GetQueryable().Any(where);
if (isAny)
{
return new BusinessResponse<bool>(true, "已存在", "Exist_Data")
{
Data = true
};
} return new BusinessResponse<bool>(false, "不存在", "Not_Data");
} /// <summary>
/// 获得指定的查询对象
/// </summary>
/// <returns></returns>
public IQueryable<TOut> GetQueryable_Out<TOut>(Expression<Func<TOut, bool>> where) where TOut : class
{
var query = this.context.Set<TOut>().Where(where); query = SetQueryable(query); return query;
} /// <summary>
/// 获得当前的查询对象
/// </summary>
/// <returns></returns>
public IQueryable<OutT> GetQueryable_Out<OutT>() where OutT : class
{
var query = this.context.Set<OutT>().AsQueryable(); query = SetQueryable(query); return query;
} /// <summary>
/// 执行分页
/// </summary>
/// <param name="query">查询对象</param>
/// <param name="result">返回对象</param>
/// <param name="paging">分页对象</param>
public IPagingBusinessResponse<ICollection<OutT>> ExecutePage_Out<OutT>(IQueryable<OutT> query, IPagingBusinessResponse<ICollection<OutT>> result, Paging paging) where OutT : class
{
result.Data = query.Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList(); paging.Total = query.FutureCount().Value; paging.PageSize = paging.PageSize; paging.PageIndex = paging.PageIndex; result.Paging = paging; return result;
} /// <summary>
/// 获得当前的查询对象
/// </summary>
/// <returns></returns>
public virtual IQueryable<T> GetQueryable()
{
var query = this.context.Set<T>().AsQueryable(); query = SetQueryable(query); return query;
} /// <summary>
/// 获得当前的查询对象
/// </summary>
/// <returns></returns>
public virtual IQueryable<T> GetQueryable(Expression<Func<T, bool>> where)
{
var query = this.context.Set<T>().Where(where); query = SetQueryable(query); return query;
} /// <summary>
/// 获得当前的查询对象
/// </summary>
/// <returns></returns>
public IQueryable<dynamic> GetQueryable(Expression<Func<T, bool>> where, Expression<Func<T, dynamic>> selectExpression)
{
var query = this.context.Set<T>().Where(where); query = SetQueryable(query); return query.Select(selectExpression);
} /// <summary>
/// 获得当前的查询对象
/// </summary>
/// <returns></returns>
public IQueryable<TOut> GetQueryable<TOut>(Expression<Func<T, bool>> where, Expression<Func<T, TOut>> selectExpression)
{
var query = this.context.Set<T>().Where(where); query = SetQueryable(query); return query.Select(selectExpression);
} /// <summary>
/// 执行分页_动态操作
/// </summary>
/// <param name="query">查询对象</param>
/// <param name="result">返回对象</param>
/// <param name="paging">分页对象</param>
/// <param name="selectExpression"></param>
public IPagingBusinessResponse<dynamic> ExecutePage_Dynamic(IQueryable<T> query, IPagingBusinessResponse<dynamic> result, Paging paging, Expression<Func<T, dynamic>> selectExpression)
{
result.Data = query.Select(selectExpression).Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList(); paging.Total = query.FutureCount().Value; paging.PageSize = paging.PageSize; paging.PageIndex = paging.PageIndex; result.Paging = paging; return result;
} /// <summary>
/// 执行分页_动态操作
/// </summary>
/// <param name="where">查询条件</param>
/// <param name="orderLambda">排序表达式</param>
/// <param name="codeOrderType">排序方式</param>
/// <param name="paging">分页参数</param>
/// <param name="selectExpression">动态类型</param>
/// <returns></returns>
public IPagingBusinessResponse<dynamic> ExecutePage_Dynamic<TKey>(Expression<Func<T, bool>> where, Expression<Func<T, TKey>> orderLambda, CodeOrderType codeOrderType, Paging paging, Expression<Func<T, dynamic>> selectExpression)
{ var query = this.GetQueryable(where); if (codeOrderType == CodeOrderType.Asc)
{
query = query.OrderBy(orderLambda);
}
else if (codeOrderType == CodeOrderType.Desc)
{
query = query.OrderByDescending(orderLambda);
} var result = new PagingBusinessResponse<dynamic>
{
Data = query.Select(selectExpression).Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList()
}; paging.Total = query.FutureCount().Value; paging.PageSize = paging.PageSize; paging.PageIndex = paging.PageIndex; result.Paging = paging; return result;
} /// <summary>
/// 执行分页
/// <para>返回动态对象</para>
/// </summary>
/// <param name="query">查询对象</param>
/// <param name="paging">分页对象</param>
/// <param name="selectExpression">动态表达式</param>
public IPagingBusinessResponse<dynamic> ExecutePage_Dynamic(IQueryable<T> query, Paging paging, Expression<Func<T, dynamic>> selectExpression)
{
var result = new PagingBusinessResponse<dynamic>
{
Data = query.Select(selectExpression).Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList()
}; paging.Total = query.FutureCount().Value; paging.PageSize = paging.PageSize; paging.PageIndex = paging.PageIndex; result.Paging = paging; return result;
} /// <summary>
/// 执行分页
/// </summary>
/// <param name="query">查询对象</param>
/// <param name="result">返回对象</param>
/// <param name="paging">分页对象</param>
public IPagingBusinessResponse<ICollection<T>> ExecutePage(IQueryable<T> query, IPagingBusinessResponse<ICollection<T>> result, Paging paging)
{
result.Data = query.Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList(); paging.Total = query.FutureCount().Value; paging.PageSize = paging.PageSize; paging.PageIndex = paging.PageIndex; result.Paging = paging; return result;
} /// <summary>
/// 执行分页
/// </summary>
/// <param name="query">查询对象</param>
/// <param name="paging">分页对象</param>
public IPagingBusinessResponse<ICollection<T>> ExecutePage(IQueryable<T> query, Paging paging)
{
var result = new PagingBusinessResponse<ICollection<T>>
{
Data = query.Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList()
}; paging.Total = query.FutureCount().Value; paging.PageSize = paging.PageSize; paging.PageIndex = paging.PageIndex; result.Paging = paging; return result;
} /// <summary>
/// 执行分页
/// <para>返回传入的类型</para>
/// </summary>
/// <typeparam name="Tout">返回的类型</typeparam>
/// <param name="query">当前的查询对象</param>
/// <param name="paging">分页参数</param>
/// <param name="selectExpression">查询表达式</param>
/// <returns></returns>
public IPagingBusinessResponse<ICollection<Tout>> ExecutePage<Tout>(IQueryable<T> query, Paging paging, Expression<Func<T, Tout>> selectExpression)
{
var result = new PagingBusinessResponse<ICollection<Tout>>
{
Data = query.Select(selectExpression).Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList()
}; // paging.Total = query.FutureCount().Value; paging.Total = query.Count(); paging.PageSize = paging.PageSize; paging.PageIndex = paging.PageIndex; result.Paging = paging; return result;
} /// <summary>
/// 获得题目下的所有题意理解
/// </summary>
/// <param name="id">编号</param>
/// <returns></returns>
public IBusinessResponse<T> GetById(dynamic id)
{
var entity = context.Set<T>().Find(id); if (entity == null)
{
return new BusinessResponse<T>(false, "编号" + id + "不存在", "Not_Data");
} return new BusinessResponse<T>(entity);
} /// <summary>
/// Executes the select SQL.
/// <para>执行查询语句</para>
/// <para>返回TIn 泛型单个对象</para>
/// </summary>
/// <typeparam name="TIn">返回映射泛型</typeparam>
/// <param name="sql">The SQL.</param>
/// <returns></returns>
public TIn ExecSelectSql<TIn>(string sql)
{
return context.Database.SqlQuery<TIn>(sql).FirstOrDefault();
} /// <summary>
/// Executes the select SQL.
/// <para>执行查询语句</para>
/// <para>返回TIn 泛型单个对象</para>
/// </summary>
/// <typeparam name="TIn">返回映射泛型</typeparam>
/// <param name="sql">The SQL.</param>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public TIn ExecSelectSql<TIn>(string sql, params object[] parameters)
{ return context.Database.SqlQuery<TIn>(sql, parameters).FirstOrDefault();
} /// <summary>
/// Executes the select SQL to list.
/// <para>执行查询语句并返回集合列表</para>
/// <para>返回TIn 泛型集合对象</para>
/// </summary>
/// <typeparam name="TIn">返回映射泛型</typeparam>
/// <param name="sql">The SQL.</param>
/// <returns></returns>
public List<TIn> ExecSelectSqlToList<TIn>(string sql)
{
return context.Database.SqlQuery<TIn>(sql).ToList();
} /// <summary>
/// Executes the select SQL to list.
/// <para>执行查询语句并返回集合列表</para>
/// <para>返回TIn 泛型集合对象</para>
/// </summary>
/// <typeparam name="TIn">返回映射泛型</typeparam>
/// <param name="sql">The SQL.</param>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public List<TIn> ExecSelectSqlToList<TIn>(string sql, params object[] parameters)
{
return context.Database.SqlQuery<TIn>(sql, parameters).ToList();
} /// <summary>
/// Executes the command SQL
/// <para>执行命令的SQL</para>
/// </summary>
/// <typeparam name="TIn">The type of the in.</typeparam>
/// <param name="sql">The SQL.</param>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public int ExecCommandSql<TIn>(string sql, params object[] parameters)
{
return context.Database.ExecuteSqlCommand(sql, parameters);
} /// <summary>
/// 根据主键批量物理删除
/// </summary>
/// <param name="where">where条件</param>
/// <returns></returns>
public IBusinessResponse<bool> Remove(Expression<Func<T, bool>> where)
{
return this.Remove(GetQueryable(where));
} /// <summary>
/// 根据主键批量物理删除
/// </summary>
/// <param name="queryable">可查询对象</param>
/// <returns></returns>
public IBusinessResponse<bool> Remove(IQueryable<T> queryable)
{
var rowNumber = queryable.Delete(); if (rowNumber == 0)
{
return new BusinessResponse<bool>(false, "删除失败", "Not_Row", false);
}
return new BusinessResponse<bool>(true, "删除成功", "Success", true);
} }

  

entity framework 封装基类的更多相关文章

  1. Python+Selenium框架设计之框架内封装基类和实现POM

    原文地址https://blog.csdn.net/u011541946/article/details/70269965 作者:Anthony_tester 来源:CSDN    博客地址https ...

  2. 微信公众号开发系列-Http请求封装基类

    HttpHelper请求封装基类,支持get请求和POS请求,方便微信开发接口交互,为后面接口交互做准备. 1.HttpHelper帮助基类 [csharp] view plaincopy using ...

  3. Http请求封装基类HttpHelper.cs

    HttpHelper请求封装基类,支持get请求和POS请求http接口交互,为后面接口交互做准备. 1.HttpHelper帮助基类 using System; using System.Colle ...

  4. Asp.Net MVC 使用Entity Framework创建模型类

    先来说说LINQ to SQL和Entity Framework的区别: LINQ to SQL和Entity Framework都是一种包含LINQ功能的对象关系映射技术.他们之间的本质区别在于EF ...

  5. Asp.Net MVC 模型(使用Entity Framework创建模型类) - Part.1

    这篇教程的目的是解释在创建ASP.NET MVC应用程序时,如何使用Microsoft Entity Framework来创建数据访问类.这篇教程假设你事先对Microsoft Entity Fram ...

  6. Asp.Net MVC 模型(使用Entity Framework创建模型类)

    这篇教程的目的是解释在创建ASP.NET MVC应用程序时,如何使用Microsoft Entity Framework来创建数据访问类.这篇教程假设你事先对Microsoft Entity Fram ...

  7. Entity FrameWork 6帮助类

    public class BaseDAL { string strConn = ""; public BaseDAL(string connString) { strConn = ...

  8. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  9. Entity Framework快速入门--直接修改(简要介绍ObjectContext处理机制)

    在介绍Entity Framework的修改实体到数据库的方法之前呢,我们先简要的介绍一下ObjectContext的处理机制. 1.ObjectContext的处理机制 ObjectContext是 ...

随机推荐

  1. Android 软键盘监听事件

    Android软键盘的隐藏显示研究 Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入.     那么,弹出软键盘后必然会造成原有布局高度的减少 ...

  2. mysql的四种启动方式

    查看该版本的相应参数: mysqld --verbose --help     1.mysqld  ./mysqld --defaults-file=/etc/my.cnf --user=mysql ...

  3. dom对象常用的属性和方法有哪些?

    dom对象常用的属性和方法有哪些? 一.总结 一句话总结: 1.document属性和方法:document的属性有head,body之类,方法有各种获取element的方法 2.element的属性 ...

  4. js的dom对象(带实例超详细全解)

    js的dom对象(带实例超详细全解) 一.总结 一句话总结: 1.DOM中的方法区分大小写么? 解答:区分 2.DOM中元素和节点的关系式什么? 解答:元素就是标签,节点中有元素节点,也是标签,节点中 ...

  5. android通用適配器

    一.需求分析 在寻常的android开发过程中.ListView.GridView适配的编写是一件非常麻烦并且非常反复的事情,每次都须要考虑性能的优化.item的编写.获取网络图片时候信息的错乱等问题 ...

  6. 输入框改变placeholder字体颜色

    ::-webkit-input-placeholder {  color: red;}:-moz-placeholder {  color: red;}::-moz-placeholder{color ...

  7. linux下jar包的生存与开机自动启动

    前言 作为一名Android开发,有的时候也是要做做其他工作的,前段时间写了一个很简单的java消息转发程序,因为和三方厂商合作,对方只提供了java的sdk,然而我们这边都是清一色的C#后台开发人员 ...

  8. Swift下CoreData的使用

    我之前的随笔中有写过一些iOS持久化存储的方法,包含了sqlite.解归档.沙盒存放等等.这些方式中,能够大规模存储,并保持性能的只有使用sqlite了.而这里将记录下Cocoa自身继承的数据库的存储 ...

  9. An HTTP & HTTP/2 client for Android and Java applications OkHttp

    HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP effic ...

  10. hexo改造

    一直在思考网站分类的问题. 用hexo默认的分类,并不利于用户直观的感受到网站的内容意图,尤其是hexo首页进入后是最近发表的文章列表.然后考虑开启多个hexo服务,每个hexo服务是一个分类内容,如 ...