From 9657fd730cfec576baf150fd4cb0b31022633fea Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 3 Jul 2023 20:12:36 +0800 Subject: [PATCH] Add queryable.OrderByPropertyName --- .../QueryableProvider/QueryableProvider.cs | 20 +++++++++++++++++++ Src/Asp.Net/SqlSugar/Interface/IQueryable.cs | 1 + 2 files changed, 21 insertions(+) diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 9a72ddcfe..4a9953b66 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1044,6 +1044,26 @@ namespace SqlSugar this.QueryBuilder.SampleBy = sql; return this; } + public ISugarQueryable OrderByPropertyName(string orderPropertyName, OrderByType? orderByType = null) + { + if (orderPropertyName != null) + { + if (this.Context.EntityMaintenance.GetEntityInfoWithAttr(typeof(T)).Columns.Any(it => + it.DbColumnName?.EqualCase(orderPropertyName)==true + || it.PropertyName?.EqualCase(orderPropertyName)==true)) + { + var name = this.Context.EntityMaintenance.GetEntityInfoWithAttr(typeof(T)).Columns.FirstOrDefault(it => + it.DbColumnName?.EqualCase(orderPropertyName) == true + || it.PropertyName?.EqualCase(orderPropertyName) == true)?.DbColumnName; + return this.OrderBy(this.SqlBuilder.GetTranslationColumnName( name) + " "+orderByType); + } + else + { + Check.ExceptionEasy($"OrderByPropertyName error.{orderPropertyName} does not exist in the entity class", $"OrderByPropertyName出错实体类中不存在{orderPropertyName}"); + } + } + return this; + } public virtual ISugarQueryable OrderBy(string orderFileds) { orderFileds = orderFileds.ToCheckField(); diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index 2b458a181..408a07a29 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -105,6 +105,7 @@ namespace SqlSugar ISugarQueryable InIF(bool isWhere,Expression> expression, ISugarQueryable childQueryExpression); ISugarQueryable OrderBy(string orderFileds); + ISugarQueryable OrderByPropertyName(string orderPropertyName,OrderByType? orderByType=null); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderByDescending(Expression> expression); ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds);