mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-24 04:53:45 +08:00
Subquery join
This commit is contained in:
@@ -125,7 +125,9 @@ namespace OrmTest
|
|||||||
var list2 = db.Queryable<Order>().Where(it =>
|
var list2 = db.Queryable<Order>().Where(it =>
|
||||||
SqlFunc.Subqueryable<OrderItem>()
|
SqlFunc.Subqueryable<OrderItem>()
|
||||||
.LeftJoin<OrderItem>((i,y)=>i.ItemId==y.ItemId)
|
.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();
|
).ToList();
|
||||||
|
|
||||||
Console.WriteLine("#### Subquery End ####");
|
Console.WriteLine("#### Subquery End ####");
|
||||||
|
@@ -205,7 +205,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
void ThrowTrue(bool isError)
|
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
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public class SubWhere: ISubOperation
|
public class SubWhere : ISubOperation
|
||||||
{
|
{
|
||||||
public bool HasWhere
|
public bool HasWhere
|
||||||
{
|
{
|
||||||
@@ -34,14 +34,14 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public ExpressionContext Context
|
public ExpressionContext Context
|
||||||
{
|
{
|
||||||
get;set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetValue(Expression expression)
|
public string GetValue(Expression expression)
|
||||||
{
|
{
|
||||||
var exp = expression as MethodCallExpression;
|
var exp = expression as MethodCallExpression;
|
||||||
var argExp= exp.Arguments[0];
|
var argExp = exp.Arguments[0];
|
||||||
var result= "WHERE "+SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
|
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
|
||||||
|
|
||||||
|
|
||||||
var regex = @"^WHERE (\@Const\d+) $";
|
var regex = @"^WHERE (\@Const\d+) $";
|
||||||
@@ -59,8 +59,9 @@ namespace SqlSugar
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name)+UtilConstants.Dot;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -55,7 +55,8 @@ namespace SqlSugar
|
|||||||
var argExp = exp.Arguments[1];
|
var argExp = exp.Arguments[1];
|
||||||
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple); ;
|
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple); ;
|
||||||
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,6 +17,7 @@ namespace SqlSugar
|
|||||||
new SubWhere(){ Context=Context },
|
new SubWhere(){ Context=Context },
|
||||||
new SubWhereIF(){ Context=Context },
|
new SubWhereIF(){ Context=Context },
|
||||||
new SubLeftJoin(){ Context=Context },
|
new SubLeftJoin(){ Context=Context },
|
||||||
|
new SubInnerJoin(){ Context=Context },
|
||||||
new SubAnd(){ Context=Context },
|
new SubAnd(){ Context=Context },
|
||||||
new SubAndIF(){ Context=Context },
|
new SubAndIF(){ Context=Context },
|
||||||
new SubAny(){ Context=Context },
|
new SubAny(){ Context=Context },
|
||||||
|
@@ -92,6 +92,7 @@
|
|||||||
<Compile Include="Entities\SubInsertTree.cs" />
|
<Compile Include="Entities\SubInsertTree.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Common\MapperExpression.cs" />
|
<Compile Include="ExpressionsToSql\Common\MapperExpression.cs" />
|
||||||
<Compile Include="ExpressionsToSql\ResolveItems\MapperExpressionResolve.cs" />
|
<Compile Include="ExpressionsToSql\ResolveItems\MapperExpressionResolve.cs" />
|
||||||
|
<Compile Include="ExpressionsToSql\Subquery\Items\SubInnerJoin.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubLeftJoin.cs" />
|
<Compile Include="ExpressionsToSql\Subquery\Items\SubLeftJoin.cs" />
|
||||||
<Compile Include="OnlyNet\Compatible.cs" />
|
<Compile Include="OnlyNet\Compatible.cs" />
|
||||||
<Compile Include="OnlyNet\KdbndpInserttable.cs" />
|
<Compile Include="OnlyNet\KdbndpInserttable.cs" />
|
||||||
|
Reference in New Issue
Block a user