navigate add whereif

This commit is contained in:
sunkaixuan
2022-04-14 19:22:48 +08:00
parent de0f23046c
commit 6d902fdadb
4 changed files with 39 additions and 5 deletions

View File

@@ -86,9 +86,9 @@ namespace OrmTest
db.Insertable(new ABMapping1() { AId=1,BId=1 }).ExecuteCommand();
db.Insertable(new ABMapping1() { AId =2, BId = 1 }).ExecuteCommand();
db.Insertable(new ABMapping1() { AId = 2, BId = 2 }).ExecuteCommand();
var p = "";
var list3= db.Queryable<A1>()
.Includes(x => x.BList.Where(it=>it.Id==1).ToList())
.Includes(x => x.BList.WhereIF(!string.IsNullOrEmpty(p),it=>it.Id==11).ToList())
.Where(x=>x.BList.Any()).ToList();
var list31 = db.Queryable<A1>().Includes(x => x.BList,x=>x.AList).ToList();

View File

@@ -279,6 +279,15 @@ namespace SqlSugar
var exp = method.Arguments[1];
where.Add(" " +queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString());
}
if (method.Method.Name == "WhereIF")
{
var isOk = LambdaExpression.Lambda(method.Arguments[1]).Compile().DynamicInvoke();
if (isOk.ObjToBool())
{
var exp = method.Arguments[2];
where.Add(" " + queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString());
}
}
else if (method.Method.Name == "OrderBy")
{
var exp = method.Arguments[1];

View File

@@ -254,6 +254,7 @@
<Compile Include="SqlSugarClient.cs" />
<Compile Include="Utilities\CallContext.cs" />
<Compile Include="Utilities\CallContextAsync.cs" />
<Compile Include="Utilities\CommonExtensions.cs" />
<Compile Include="Utilities\PropertyCallAdapterProvider.cs" />
<Compile Include="Utilities\SugarRetry.cs" />
<Compile Include="Utilities\DataTableExtensions.cs" />

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public static class CommonExtensions
{
public static IEnumerable<T> WhereIF<T>(this IEnumerable<T> thisValue, bool isOk, Func<T, bool> predicate)
{
if (isOk)
{
return thisValue.Where(predicate);
}
else
{
return thisValue;
}
}
}
}