mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 04:35:29 +08:00
Update nav query
This commit is contained in:
parent
0a1326506d
commit
fdc26fdf60
@ -9,6 +9,7 @@ namespace SqlSugar
|
||||
|
||||
internal class SqlInfo
|
||||
{
|
||||
public bool IsSelectNav { get; set; }
|
||||
public int? Take { get; set; }
|
||||
public int? Skip { get; set; }
|
||||
public string WhereString { get; set; }
|
||||
|
@ -856,6 +856,18 @@ namespace SqlSugar
|
||||
{
|
||||
var pkInfo = entityInfo.Columns.FirstOrDefault(x => x.IsPrimarykey);
|
||||
result.SelectString = (" " + queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.SelectSingle).GetString());
|
||||
if (ExpressionTool.ContainsTwoLevelAccess(exp))
|
||||
{
|
||||
var shortName = ExpressionTool.GetParameters(exp).FirstOrDefault()?.Name;
|
||||
if (shortName.HasValue())
|
||||
{
|
||||
if (result.TableShortName == null)
|
||||
{
|
||||
result.TableShortName = shortName;
|
||||
result.IsSelectNav = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pkInfo != null)
|
||||
{
|
||||
var pkName = pkInfo.DbColumnName;
|
||||
@ -941,6 +953,13 @@ namespace SqlSugar
|
||||
private static void AppColumns(SqlInfo result, ISugarQueryable<object> queryable, string columnName)
|
||||
{
|
||||
var selectPkName = queryable.SqlBuilder.GetTranslationColumnName(columnName);
|
||||
if (result.IsSelectNav)
|
||||
{
|
||||
if (result.SelectString != null && !result.SelectString.ToLower().Contains($" {selectPkName.ToLower()} AS {selectPkName.ToLower()}"))
|
||||
{
|
||||
result.SelectString = result.SelectString + "," + (selectPkName + " AS " + selectPkName);
|
||||
}
|
||||
}
|
||||
if (result.SelectString!=null && !result.SelectString.ToLower().Contains(selectPkName.ToLower()))
|
||||
{
|
||||
result.SelectString = result.SelectString + "," + (selectPkName +" AS "+ selectPkName);
|
||||
|
@ -10,6 +10,32 @@ namespace SqlSugar
|
||||
{
|
||||
public class ExpressionTool
|
||||
{
|
||||
public static bool ContainsTwoLevelAccess(Expression exp)
|
||||
{
|
||||
var result = false;
|
||||
|
||||
if (exp is LambdaExpression lambda &&
|
||||
lambda.Body is MemberInitExpression initExpr)
|
||||
{
|
||||
var param = lambda.Parameters[0];
|
||||
|
||||
foreach (var binding in initExpr.Bindings)
|
||||
{
|
||||
if (binding is MemberAssignment assign)
|
||||
{
|
||||
if (assign.Expression is MemberExpression outer &&
|
||||
outer.Expression is MemberExpression inner &&
|
||||
inner.Expression == param)
|
||||
{
|
||||
result = true;
|
||||
break; // 已经找到了,就退出循环
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string GetMemberNameByMethod(Expression expression, string name)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user