mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Subquery join
This commit is contained in:
parent
f17754c6f9
commit
8c9c3703ee
@ -125,7 +125,9 @@ namespace OrmTest
|
||||
var list2 = db.Queryable<Order>().Where(it =>
|
||||
SqlFunc.Subqueryable<OrderItem>()
|
||||
.LeftJoin<OrderItem>((i,y)=>i.ItemId==y.ItemId)
|
||||
.Where<OrderItem>((i,y) => y.ItemId== it.Id).Any()
|
||||
.InnerJoin<OrderItem>((i,z) => i.ItemId == z.ItemId)
|
||||
.Where(i=>i.ItemId==1)
|
||||
.Any()
|
||||
).ToList();
|
||||
|
||||
Console.WriteLine("#### Subquery End ####");
|
||||
|
@ -205,7 +205,7 @@ namespace SqlSugar
|
||||
}
|
||||
void ThrowTrue(bool isError)
|
||||
{
|
||||
Check.Exception(isError, ErrorMessage.GetThrowMessage(expression.ToString() + "no support", "不支持表达式" + expression.ToString()));
|
||||
Check.Exception(isError, ErrorMessage.GetThrowMessage(expression.ToString() + "no support", "不支持表达式" + expression.ToString()+ " 1.检查当前表达式中的别名是否与Mapper中的一致 2.目前只支持 1对1 Mapper下的 Where "));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,62 @@
|
||||
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 301;
|
||||
}
|
||||
}
|
||||
|
||||
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[1];
|
||||
Context.InitMappingInfo(parameter.Type);
|
||||
var tableName= Context.GetTranslationTableName(parameter.Type.Name, true);
|
||||
var joinString =string.Format(" {2} INNER JOIN {1} {0} ",
|
||||
this.Context.GetTranslationColumnName(parameter.Name),
|
||||
tableName,
|
||||
this.Context.JoinIndex==0?name:"");
|
||||
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++;
|
||||
|
||||
//result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubWhere: ISubOperation
|
||||
public class SubWhere : ISubOperation
|
||||
{
|
||||
public bool HasWhere
|
||||
{
|
||||
@ -34,14 +34,14 @@ namespace SqlSugar
|
||||
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
get; set;
|
||||
}
|
||||
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
var exp = expression as MethodCallExpression;
|
||||
var argExp= exp.Arguments[0];
|
||||
var result= "WHERE "+SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
|
||||
var argExp = exp.Arguments[0];
|
||||
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
|
||||
|
||||
|
||||
var regex = @"^WHERE (\@Const\d+) $";
|
||||
@ -59,8 +59,9 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
|
||||
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name)+UtilConstants.Dot;
|
||||
result = result.Replace(selfParameterName,SubTools.GetSubReplace(this.Context));
|
||||
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
|
||||
if (this.Context.JoinIndex == 0)
|
||||
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,8 @@ namespace SqlSugar
|
||||
var argExp = exp.Arguments[1];
|
||||
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple); ;
|
||||
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
|
||||
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
|
||||
if (this.Context.JoinIndex == 0)
|
||||
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ namespace SqlSugar
|
||||
new SubWhere(){ Context=Context },
|
||||
new SubWhereIF(){ Context=Context },
|
||||
new SubLeftJoin(){ Context=Context },
|
||||
new SubInnerJoin(){ Context=Context },
|
||||
new SubAnd(){ Context=Context },
|
||||
new SubAndIF(){ Context=Context },
|
||||
new SubAny(){ Context=Context },
|
||||
|
@ -92,6 +92,7 @@
|
||||
<Compile Include="Entities\SubInsertTree.cs" />
|
||||
<Compile Include="ExpressionsToSql\Common\MapperExpression.cs" />
|
||||
<Compile Include="ExpressionsToSql\ResolveItems\MapperExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubInnerJoin.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubLeftJoin.cs" />
|
||||
<Compile Include="OnlyNet\Compatible.cs" />
|
||||
<Compile Include="OnlyNet\KdbndpInserttable.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user