Support Subquery.Having

This commit is contained in:
sunkaixuna
2021-09-10 15:30:48 +08:00
parent 05cd2a3c5c
commit b449a446f8
5 changed files with 64 additions and 3 deletions

View File

@@ -153,6 +153,7 @@ namespace OrmTest
JoinType.Left, o.Name == SqlFunc.ToString(SqlFunc.MergeString(",", i.Name, ","))
))
.Select<ViewOrder>().ToList();
var test16 = db.Queryable<Order>().Select(it => SqlFunc.SqlServer_DateDiff("day", DateTime.Now.AddDays(-1), DateTime.Now)).ToList();
Console.WriteLine("#### Examples End ####");
}
@@ -242,8 +243,11 @@ namespace OrmTest
.Where(i=>i.ItemId==1)
.Any()
).ToList();
var list3=db.Queryable<Order>().Select(it => SqlFunc.SqlServer_DateDiff("day", DateTime.Now.AddDays(-1), DateTime.Now)).ToList();
;
var list3 = db.Queryable<Order>().Select(it => new
{
customName = SqlFunc.Subqueryable<Custom>().Where(s=>s.Id==it.CustomId).GroupBy(s=>s.Name).Having(s=>SqlFunc.AggregateCount(s.Id)>0).Select(s => s.Name)
}).ToList();
Console.WriteLine("#### Subquery End ####");
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar
{
public class SubHaving : ISubOperation
{
public bool HasWhere
{
get; set;
}
public string Name
{
get { return "Having"; }
}
public Expression Expression
{
get; set;
}
public int Sort
{
get
{
return 480;
}
}
public ExpressionContext Context
{
get; set;
}
public string GetValue(Expression expression)
{
var exp = expression as MethodCallExpression;
var argExp = exp.Arguments[0];
var result = "Having " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
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;
}
}
}

View File

@@ -32,7 +32,8 @@ namespace SqlSugar
new SubOrderBy(){ Context=Context },
new SubOrderByDesc(){ Context=Context },
new SubGroupBy(){ Context=Context},
new SubAs(){Context=Context}
new SubAs(){Context=Context},
new SubHaving(){ Context=Context}
};
}

View File

@@ -31,6 +31,10 @@ namespace SqlSugar
{
return this;
}
public Subqueryable<T> Having(Func<T, bool> expression)
{
return this;
}
public Subqueryable<T> Where<Main, Join1>(Func<Main, Join1, bool> expression)
{
return this;

View File

@@ -90,6 +90,7 @@
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
<Compile Include="Entities\SqlSguarTransaction.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubAs.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubHaving.cs" />
<Compile Include="Interface\ISugarRepository.cs" />
<Compile Include="Interface\IParameterInsertable.cs" />
<Compile Include="Abstract\InsertableProvider\ParameterInsertable.cs" />