zhengyuxuan
2025-04-03 06f7ccdea12e211d05f6eef75e6e2fb4b493377c
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
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Linq.Expressions;
 
namespace LifePayment.Domain.Shared;
 
public static class IQueryableExtensions
{
    /// <summary>
    /// 当条件为true时为IQueryable附加where,注:如果条件为true会更新source
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="source"></param>
    /// <param name="condition"></param>
    /// <param name="wherePredicate"></param>
    /// <returns></returns>
    public static IQueryable<T> WhereIfWithUpdateSource<T>(this IQueryable<T> source, bool condition, Expression<Func<T, bool>> wherePredicate)
    {
        if (condition)
            source = source.Where(wherePredicate);
 
        return source;
    }
 
    /// <summary>
    /// 当条件为true时为IQueryable附加include,注:如果条件为true会更新source
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TP"></typeparam>
    /// <param name="source"></param>
    /// <param name="condition"></param>
    /// <param name="navigationPropertyPath"></param>
    /// <returns></returns>
    public static IQueryable<T> IncludeIf<T, TP>(this IQueryable<T> source, bool condition, Expression<Func<T, TP>> navigationPropertyPath) where T : class
    {
        if (condition)
            source = source.Include(navigationPropertyPath);
 
        return source;
    }
}