/// <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. nopCommerce 3.9 大波浪系列 之 使用部署在Docker中的Redis缓存主从服务

    一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集群. 一般来说,只有一台Redis是不可行的,原因如下: 单台Redis服务器会发 ...

  2. [Typescript] Sorting arrays in TypeScript

    In this lesson we cover all the details of how to sort a list of items using TypeScript. We also pre ...

  3. 在nginx中使用lua直接訪问mysql和memcaced达到数据接口的统一

    安装nginx參见<nginx+lua+redis构建高并发应用> 让nginx 中的nginx_lua_module支持mysql 和memcache 下载 https://github ...

  4. Android 设置alpha值来制作透明与渐变效果的实例

    Android系统支持的颜色是由4个值组成的,前3个为RGB,也就是我们常说的三原色(红.绿.蓝),最后一个值是A,也就是Alpha.这4个值都在0~255之间.颜色值越小,表示该颜色越淡,颜色值越大 ...

  5. [Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions

    In this lesson we'll see how Ramda's path and pathOr functions can be used to safely access a deeply ...

  6. oracle11g 在azure云中使用rman进行实例迁移

    1,開始备份 备份脚本rman_full_backup.sh内容例如以下: #!/bin/sh export DATE=`date +%F` export BACK_DIR='/backupdisk/ ...

  7. 【9309】求Y=X1/3

    Time Limit: 1 second Memory Limit: 2 MB 问题描述 求Y=X1/3次方的值.X由键盘输入(x不等于0,在整型范围内).利用下列迭代公式计算: yn + 1=2/3 ...

  8. [转]erlang ranch

    一. ranch app启动: ranch_sup -> ranch_server % 创建ets, 并提供接口给其他进程读写 二. 启动diy app (监听模块: 用ranch_tcp -& ...

  9. 收集 天创恒达高清采集卡TC-5A0N7

    版权声明:本文博客琅邪工作室原创文章,博客,未经同意不得转载.

  10. 数据库版本管理工具Flyway——基础篇

    Flyway 默认规约 SQL 脚本文件默认位置是项目的源文件夹下的db/migration 目录. Java 代码默认位于db.migration 包. SQL 脚本文件及Java 代码类名必须遵循 ...