mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Add Subquery.WhereIF
This commit is contained in:
parent
feb5a17d0a
commit
449873625e
@ -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()
|
||||
|
@ -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 第一个参数不支持表达式中的变量,只支持外部变量");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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 },
|
||||
|
@ -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;
|
||||
|
@ -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…
Reference in New Issue
Block a user