Tjiny
2025-07-01 4695a1e617430e72f06e1f6cea55cda3b65a77a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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();
    }
}