mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Add multi-table query syntax sugar
This commit is contained in:
parent
3396e12c0a
commit
b6684e8b73
@ -400,6 +400,15 @@ namespace OrmTest
|
||||
|
||||
).Select(o=>o).ToList();
|
||||
|
||||
|
||||
var query5 = db.Queryable<Order>()
|
||||
.InnerJoin<Custom>((o, cus) => o.CustomId == cus.Id)
|
||||
.InnerJoin<OrderItem>((o, cus, oritem) => o.Id == oritem.OrderId)
|
||||
.Where((o) => o.Id == 1)
|
||||
.Select((o, cus) => new ViewOrder { Id=o.Id, CustomName = cus.Name })
|
||||
.ToList();
|
||||
|
||||
|
||||
Console.WriteLine("#### Join Table End ####");
|
||||
}
|
||||
|
||||
|
@ -46,6 +46,23 @@ namespace SqlSugar
|
||||
return this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
}
|
||||
}
|
||||
public ISugarQueryable<T, T2> LeftJoin<T2>(Expression<Func<T, T2, bool>> joinExpression)
|
||||
{
|
||||
var result = InstanceFactory.GetQueryable<T, T2>(this.Context.CurrentConnectionConfig);
|
||||
result.SqlBuilder = this.SqlBuilder;
|
||||
result.Context = this.Context;
|
||||
result.QueryBuilder.JoinQueryInfos.Add(GetJoinInfo(joinExpression,JoinType.Left));
|
||||
return result;
|
||||
}
|
||||
|
||||
public ISugarQueryable<T, T2> InnerJoin<T2>(Expression<Func<T, T2, bool>> joinExpression)
|
||||
{
|
||||
var result = InstanceFactory.GetQueryable<T, T2>(this.Context.CurrentConnectionConfig);
|
||||
result.SqlBuilder = this.SqlBuilder;
|
||||
result.Context = this.Context;
|
||||
result.QueryBuilder.JoinQueryInfos.Add(GetJoinInfo(joinExpression, JoinType.Inner));
|
||||
return result;
|
||||
}
|
||||
public void Clear()
|
||||
{
|
||||
QueryBuilder.Clear();
|
||||
@ -1578,6 +1595,29 @@ namespace SqlSugar
|
||||
}
|
||||
return result;
|
||||
}
|
||||
protected JoinQueryInfo GetJoinInfo(Expression joinExpression,JoinType joinType)
|
||||
{
|
||||
QueryBuilder.CheckExpressionNew(joinExpression, "Join");
|
||||
QueryBuilder.JoinExpression = joinExpression;
|
||||
var express= LambdaExpression.Lambda(joinExpression).Body;
|
||||
var lastPareamter= (express as LambdaExpression).Parameters.Last();
|
||||
var expResult = this.QueryBuilder.GetExpressionValue(joinExpression, ResolveExpressType.WhereMultiple);
|
||||
this.Context.InitMappingInfo(lastPareamter.Type);
|
||||
var result= new JoinQueryInfo()
|
||||
{
|
||||
JoinIndex = QueryBuilder.JoinQueryInfos.Count,
|
||||
JoinType = joinType,
|
||||
JoinWhere = expResult.GetResultString(),
|
||||
ShortName= lastPareamter.Name,
|
||||
TableName=this.Context.EntityMaintenance.GetTableName(lastPareamter.Type)
|
||||
};
|
||||
if (result.JoinIndex == 0)
|
||||
{
|
||||
var firstPareamter = (express as LambdaExpression).Parameters.First();
|
||||
this.QueryBuilder.TableShortName = firstPareamter.Name;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void _CountEnd(MappingTableList expMapping)
|
||||
{
|
||||
@ -2448,6 +2488,23 @@ namespace SqlSugar
|
||||
#region T2
|
||||
public partial class QueryableProvider<T, T2> : QueryableProvider<T>, ISugarQueryable<T, T2>
|
||||
{
|
||||
public ISugarQueryable<T, T2,T3> LeftJoin<T3>(Expression<Func<T, T2,T3, bool>> joinExpression)
|
||||
{
|
||||
var result = InstanceFactory.GetQueryable<T, T2,T3>(this.Context.CurrentConnectionConfig);
|
||||
result.SqlBuilder = this.SqlBuilder;
|
||||
result.Context = this.Context;
|
||||
result.QueryBuilder.JoinQueryInfos.Add(GetJoinInfo(joinExpression, JoinType.Left));
|
||||
return result;
|
||||
}
|
||||
|
||||
public ISugarQueryable<T, T2,T3> InnerJoin<T3>(Expression<Func<T, T2,T3, bool>> joinExpression)
|
||||
{
|
||||
var result = InstanceFactory.GetQueryable<T, T2,T3>(this.Context.CurrentConnectionConfig);
|
||||
result.SqlBuilder = this.SqlBuilder;
|
||||
result.Context = this.Context;
|
||||
result.QueryBuilder.JoinQueryInfos.Add(GetJoinInfo(joinExpression, JoinType.Inner));
|
||||
return result;
|
||||
}
|
||||
#region Where
|
||||
public new ISugarQueryable<T, T2> Where(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
|
@ -701,5 +701,27 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
public void CheckExpressionNew(Expression expression, string methodName)
|
||||
{
|
||||
if (IsSingle() == false && this.JoinExpression != null)
|
||||
{
|
||||
var jsoinParameters = (this.JoinExpression as LambdaExpression).Parameters;
|
||||
var currentParametres = (expression as LambdaExpression).Parameters;
|
||||
if ((expression as LambdaExpression).Body.ToString() == "True")
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (currentParametres != null && currentParametres.Count > 0)
|
||||
{
|
||||
foreach (var item in currentParametres.Take(jsoinParameters.Count))
|
||||
{
|
||||
var index = currentParametres.IndexOf(item);
|
||||
var name = item.Name;
|
||||
var joinName = jsoinParameters[index].Name;
|
||||
Check.Exception(name.ToLower() != joinName.ToLower(), ErrorMessage.ExpressionCheck, joinName, methodName, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ namespace SqlSugar
|
||||
ISugarQueryable<T> AS<T2>(string tableName);
|
||||
ISugarQueryable<T> AS(string tableName);
|
||||
ISugarQueryable<T> With(string withString);
|
||||
ISugarQueryable<T,T2> LeftJoin<T2>(Expression<Func<T,T2,bool>> joinExpression);
|
||||
ISugarQueryable<T, T2> InnerJoin<T2>(Expression<Func<T, T2, bool>> joinExpression);
|
||||
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter = false);
|
||||
ISugarQueryable<T> Mapper(Action<T> mapperAction);
|
||||
ISugarQueryable<T> Mapper<AType, BType, MappingType>(Expression<Func<MappingType, ManyToMany>> expression);
|
||||
@ -179,6 +181,9 @@ namespace SqlSugar
|
||||
}
|
||||
public partial interface ISugarQueryable<T, T2> : ISugarQueryable<T>
|
||||
{
|
||||
ISugarQueryable<T, T2,T3> LeftJoin<T3>(Expression<Func<T,T2,T3,bool>> joinExpression);
|
||||
ISugarQueryable<T, T2, T3> InnerJoin<T3>(Expression<Func<T, T2, T3, bool>> joinExpression);
|
||||
|
||||
#region Where
|
||||
new ISugarQueryable<T, T2> Where(Expression<Func<T, bool>> expression);
|
||||
ISugarQueryable<T, T2> Where(Expression<Func<T, T2, bool>> expression);
|
||||
|
Loading…
Reference in New Issue
Block a user