mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-24 13:03:43 +08:00
navigate add whereif
This commit is contained in:
@@ -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();
|
||||
|
@@ -279,15 +279,24 @@ 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];
|
||||
oredrBy.Add( " "+ queryable.QueryBuilder.GetExpressionValue(exp,ResolveExpressType.WhereSingle).GetString());
|
||||
oredrBy.Add(" " + queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString());
|
||||
}
|
||||
else if (method.Method.Name == "OrderByDescending")
|
||||
{
|
||||
var exp = method.Arguments[1];
|
||||
oredrBy.Add(" " + queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString()+" DESC");
|
||||
oredrBy.Add(" " + queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString() + " DESC");
|
||||
}
|
||||
else if (method.Method.Name == "ToList")
|
||||
{
|
||||
|
@@ -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" />
|
||||
|
24
Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs
Normal file
24
Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user