JKFZJCXT/BZPT.SqlSugarRepository/SqlSugarExt.cs

66 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity.Data;
using Microsoft.Extensions.DependencyInjection;
using BZPT.Domains.Entity;
using SqlSugar;
using System.Text;
namespace BZPT.Repositories
{
public static class SqlSugarExt
{
public static SqlSugar.DbType ToSugarDbType(this DBProvider dBProvider)
{
switch (dBProvider)
{
case DBProvider.MySqlClient:
return DbType.MySql;
case DBProvider.SqlClient:
return DbType.SqlServer;
case DBProvider.OracleClient:
return DbType.Oracle;
case DBProvider.PostgreSQL:
return DbType.PostgreSQL;
case DBProvider.SQLite:
return DbType.Sqlite;
default:
return DbType.Odbc;
}
}
/// <summary>
/// 根据指定属性名称对序列进行排序
/// </summary>
/// <typeparam name="TSource">source中的元素的类型</typeparam>
/// <param name="source">一个要排序的值序列</param>
/// <param name="property">属性名称</param>
/// <param name="descending">是否降序</param>
/// <returns></returns>
public static ISugarQueryable<TEntity> OrderBy<TEntity>(this ISugarQueryable<TEntity> source, IEnumerable<SelectSort<TEntity>> sorts = null) where TEntity : IEntity, new()
{
if (sorts != null)
{
StringBuilder strOrder = new StringBuilder();
foreach (var sort in sorts)
{
if(sort.FieldExp==null)
{
throw new Exception("未指定排序表达式FieldExpSqlSugar 仓储禁用 FieldName 指定排序字段");
}
source = source.OrderBy(sort.FieldExp, sort.IsAsc ? OrderByType.Asc : OrderByType.Desc);
}
}
return source;
}
/// <summary>
/// sqlsugar
/// </summary>
/// <param name="service"></param>
public static void RegisterSqlSugar(IServiceCollection service)
{
}
}
}