using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Linq.Expressions;
|
using SqlSugar;
|
|
namespace HH.WCS.Mobox3.RiDong.util;
|
|
internal static class ExpressionHelper
|
{
|
public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
|
{
|
var invokedExpr = Expression.Invoke(right, left.Parameters);
|
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(left.Body, invokedExpr), left.Parameters);
|
}
|
public class Person
|
{
|
public string Name { get; set; }
|
public int Age { get; set; }
|
}
|
public static void test()
|
{
|
var list = new List<Person>();
|
list.Add(new Person { Age = 1, Name = "111" });
|
list.Add(new Person { Age = 1, Name = "222" });
|
list.Add(new Person { Age = 1, Name = "333" });
|
list.Add(new Person { Age = 2, Name = "111" });
|
list.Add(new Person { Age = 2, Name = "222" });
|
list.Add(new Person { Age = 2, Name = "333" });
|
list.Add(new Person { Age = 3, Name = "111" });
|
list.Add(new Person { Age = 3, Name = "222" });
|
list.Add(new Person { Age = 3, Name = "333" });
|
|
|
Expression<Func<Person, bool>> expression = x => x.Age >= 2;
|
|
expression = expression.AndAlso(x => x.Name == "111").AndAlso(x => x.Age == 3);
|
|
var data = list.Where(expression.Compile()).ToList();
|
var name = "";
|
var exp = Expressionable.Create<Person>();
|
exp.AndIF(!string.IsNullOrEmpty(name), it => it.Age == 1);
|
//exp.OrIF(!string.IsNullOrEmpty(name), it => it.Age == 1);//.OrIf 是条件成立才会拼接OR
|
exp.Or(it => it.Name.Contains("jack"));//拼接OR
|
var people = new SqlHelper<object>().GetInstance().Queryable<Person>().Where(exp.ToExpression()).ToList();
|
}
|
}
|