Browse Source

Add Subquery.WhereIF

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
449873625e
  1. 15
      Src/Asp.Net/SqlServerTest/Demos/1_Query.cs
  2. 8
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ErrorMessage.cs
  3. 62
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhereIF.cs
  4. 1
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubTools.cs
  5. 4
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Subquerable.cs
  6. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

15
Src/Asp.Net/SqlServerTest/Demos/1_Query.cs

@ -114,7 +114,20 @@ namespace OrmTest.Demo
name = it.Name,
id = SqlFunc.Subqueryable<Student>().Where(s => s.Id == it.Id).Min(s => s.Id)
}).ToList();
string name = "a";
var getAll6666 = db.Queryable<Student>().Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().WhereIF(!string.IsNullOrEmpty(name), s=>s.Id==1).Min(s => s.Id)
}).ToList();
name = null;
var getAll66666 = db.Queryable<Student>().Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().WhereIF(!string.IsNullOrEmpty(name), s => s.Id == 1).Min(s => s.Id)
}).ToList();
}
private static void Async()

8
Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ErrorMessage.cs

@ -44,5 +44,13 @@ namespace SqlSugar
return ErrorMessage.GetThrowMessage("Join {0} needs to be the same as {1} {2}", "多表查询存在别名不一致,请把{1}中的{2}改成{0}就可以了,特殊需求可以使用.Select((x,y)=>new{{ id=x.id,name=y.name}}).MergeTable().Orderby(xxx=>xxx.Id)功能将Select中的多表结果集变成单表,这样就可以不限制别名一样");
}
}
public static string WhereIFCheck
{
get
{
return ErrorMessage.GetThrowMessage("Subquery.WhereIF.IsWhere {0} not supported", "Subquery.WhereIF 第一个参数不支持表达式中的变量,只支持外部变量");
}
}
}
}

62
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhereIF.cs

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
public class SubWhereIF : ISubOperation
{
public bool HasWhere
{
get; set;
}
public string Name
{
get { return "WhereIF"; }
}
public Expression Expression
{
get; set;
}
public int Sort
{
get
{
return 400;
}
}
public ExpressionContext Context
{
get; set;
}
public string GetValue(Expression expression)
{
var exp = expression as MethodCallExpression;
object value = null;
try
{
value = ExpressionTool.DynamicInvoke(exp.Arguments[0]);
}
catch
{
Check.Exception(true, ErrorMessage.WhereIFCheck,exp.Arguments[0].ToString());
}
var isWhere= Convert.ToBoolean(value);
if (!Convert.ToBoolean(isWhere)) {
return "";
}
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, string.Empty);
return result;
}
}
}

1
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubTools.cs

@ -15,6 +15,7 @@ namespace SqlSugar
{
new SubSelect() { Context=Context },
new SubWhere(){ Context=Context },
new SubWhereIF(){ Context=Context },
new SubAnd(){ Context=Context },
new SubAny(){ Context=Context },
new SubNotAny(){ Context=Context },

4
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Subquerable.cs

@ -13,6 +13,10 @@ namespace SqlSugar
{
return this;
}
public Subqueryable<T> WhereIF(bool isWhere,Func<T, bool> expression)
{
return this;
}
public Subqueryable<T> OrderBy(Func<T, object> expression)
{
return this;

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -106,6 +106,7 @@
<Compile Include="ExpressionsToSql\Subquery\Items\SubSelectDefault.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubNotAny.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubTop.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubWhereIF.cs" />
<Compile Include="ExpressionsToSql\Subquery\Subquerable.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubWhere.cs" />
<Compile Include="ExpressionsToSql\Subquery\SubResolve.cs" />

Loading…
Cancel
Save