sqlsugar/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubInnerJoin.cs

82 lines
2.9 KiB
C#
Raw Permalink Normal View History

2025-05-11 16:20:50 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SubInnerJoin : ISubOperation
{
public bool HasWhere
{
get; set;
}
public string Name
{
get { return "InnerJoin"; }
}
public Expression Expression
{
get; set;
}
public int Sort
{
get
{
return 302;
}
}
public ExpressionContext Context
{
get; set;
}
public string GetValue(Expression expression)
{
var exp = expression as MethodCallExpression;
var argExp = exp.Arguments[0];
var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name);
var parameter = (argExp as LambdaExpression).Parameters.Last();
foreach (var item in (argExp as LambdaExpression).Parameters)
{
Context.InitMappingInfo(item.Type);
}
this.Context.RefreshMapping();
var tableName= Context.GetTranslationTableName(parameter.Type.Name, true);
if (this.Context.IsAsAttr == true)
{
var db = this.Context.SugarContext.Context;
var entityInfo = db.EntityMaintenance.GetEntityInfo(parameter.Type);
var queryable = ((QueryableProvider<object>)(db.Queryable<object>()));
tableName = queryable.GetTableName(entityInfo, tableName);
}
if (exp.Arguments.Count == 2 && exp.Arguments.Last().HasValue())
{
tableName = Context.GetTranslationTableName(ExpressionTool.DynamicInvoke(exp.Arguments.Last()) + "");
}
var joinString =string.Format(" {2} INNER JOIN {1} {0} ",
this.Context.GetTranslationColumnName(parameter.Name),
tableName,
null);
if (this.Context?.SugarContext?.Context?.CurrentConnectionConfig?.DbType==DbType.SqlServer&&this.Context?.SugarContext?.Context?.CurrentConnectionConfig?.MoreSettings?.IsWithNoLockSubquery==true)
{
joinString = $"{joinString} {SqlWith.NoLock} ";
}
var result = joinString+ "ON " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
//var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
this.Context.JoinIndex++;
new SubSelect() { Context=this.Context}.SetShortName(exp, "+");
//result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}
}