Add : SqlFunc.Subqueryable<Order>().DistinctCount(z=>z.Id)

This commit is contained in:
sunkaixuan 2022-10-10 08:46:22 +08:00
parent 73a19f8911
commit f3a1c71ba1
4 changed files with 75 additions and 1 deletions

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
public class SubDistinctCount : ISubOperation
{
public bool HasWhere
{
get; set;
}
public string Name
{
get
{
return "DistinctCount";
}
}
public Expression Expression
{
get; set;
}
public int Sort
{
get
{
return 200;
}
}
public ExpressionContext Context
{
get; set;
}
public string GetValue(Expression expression = null)
{
var exp = expression as MethodCallExpression;
var argExp = exp.Arguments[0];
var parametres = (argExp as LambdaExpression).Parameters;
if ((argExp as LambdaExpression).Body is UnaryExpression)
{
argExp = ((argExp as LambdaExpression).Body as UnaryExpression).Operand;
}
var argLambda = argExp as LambdaExpression;
if (this.Context.InitMappingInfo != null && argLambda != null && argLambda.Parameters.Count > 0)
{
foreach (var item in argLambda.Parameters)
{
this.Context.InitMappingInfo(item.Type);
}
this.Context.RefreshMapping();
}
var result = "COUNT(DISTINCT " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple) + ")";
var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}
}

View File

@ -36,7 +36,8 @@ namespace SqlSugar
new SubHaving(){ Context=Context},
new SubWithNolock(){ Context=Context },
new SubEnableTableFilter(){ Context=Context },
new SubSelectStringJoin{ Context=Context }
new SubSelectStringJoin{ Context=Context },
new SubDistinctCount{ Context=Context }
};
}

View File

@ -120,6 +120,11 @@ namespace SqlSugar
{
return default(string);
}
public int DistinctCount(Func<T, object> expression)
{
return default(int);
}
public TResult Sum<TResult>(Func<T, TResult> expression) where TResult : struct
{
return default(TResult);

View File

@ -127,6 +127,7 @@
<Compile Include="Entities\DeleteNavOptions.cs" />
<Compile Include="Entities\JoinInfoParameter.cs" />
<Compile Include="ExpressionsToSql\Common\NewExpressionInfo.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubDistinctCount.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubSelectStringJoin.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubWithNoLock.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubEnableTableFilter.cs" />