Browse Source

Add Expressionable

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
20f6501008
  1. 13
      Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs
  2. 59
      Src/Asp.Net/SqlSugar/Abstract/ExpressionableProvider/Expressionable.cs
  3. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

13
Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs

@ -19,6 +19,19 @@ namespace OrmTest.Demo
OrderBy();
SelectMerge();
ConditionalModel();
JoinExp();
}
private static void JoinExp()
{
var db = GetInstance();
var exp= Expressionable.Create<Student>()
.OrIF(1==1,it => it.Id == 11)
.And(it=>it.Id==1)
.AndIF(2==2,it => it.Id == 1)
.Or(it =>it.Name == "a1").ToExpression();
var list=db.Queryable<Student>().Where(exp).ToList();
}
private static void ConditionalModel()

59
Src/Asp.Net/SqlSugar/Abstract/ExpressionableProvider/Expressionable.cs

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
public class Expressionable<T> where T : class, new()
{
Expression<Func<T, bool>> _exp = it=>true;
public Expressionable<T> And(Expression<Func<T, bool>> exp)
{
if (_exp == null)
_exp = exp;
else
_exp = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(_exp.Body, exp.Body), _exp.Parameters);
return this;
}
public Expressionable<T> AndIF(bool isAnd, Expression<Func<T, bool>> exp)
{
if (isAnd)
And(exp);
return this;
}
public Expressionable<T> Or(Expression<Func<T, bool>> exp)
{
if (_exp == null)
_exp = exp;
else
_exp = Expression.Lambda<Func<T, bool>>(Expression.OrElse(_exp.Body, exp.Body), _exp.Parameters);
return this;
}
public Expressionable<T> OrIF(bool isOr, Expression<Func<T, bool>> exp)
{
if (isOr)
Or(exp);
return this;
}
public Expression<Func<T, bool>> ToExpression()
{
return _exp;
}
}
public class Expressionable
{
public static Expressionable<T> Create<T>() where T : class, new()
{
return new Expressionable<T>();
}
}
}

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

@ -69,6 +69,7 @@
<Compile Include="Abstract\DbMaintenanceProvider\Properties.cs" />
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />

Loading…
Cancel
Save