mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2026-03-23 16:27:20 +08:00
添加项目文件。
This commit is contained in:
38
CoreCms.Net.Model/Entities/Expression/ParameterRebinder.cs
Normal file
38
CoreCms.Net.Model/Entities/Expression/ParameterRebinder.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms
|
||||
* ProjectName: 核心内容管理系统
|
||||
* Web: https://www.corecms.net
|
||||
* Author: 大灰灰
|
||||
* Email: jianweie@163.com
|
||||
* CreateTime: 2021/1/31 21:45:10
|
||||
* Description: 暂无
|
||||
***********************************************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace CoreCms.Net.Model.Entities.Expression
|
||||
{
|
||||
public class ParameterRebinder : ExpressionVisitor
|
||||
{
|
||||
private readonly Dictionary<ParameterExpression, ParameterExpression> map;
|
||||
|
||||
public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
|
||||
{
|
||||
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
|
||||
}
|
||||
|
||||
public static System.Linq.Expressions.Expression ReplaceParameters(
|
||||
Dictionary<ParameterExpression, ParameterExpression> map, System.Linq.Expressions.Expression exp)
|
||||
{
|
||||
return new ParameterRebinder(map).Visit(exp);
|
||||
}
|
||||
|
||||
protected override System.Linq.Expressions.Expression VisitParameter(ParameterExpression p)
|
||||
{
|
||||
ParameterExpression replacement;
|
||||
if (map.TryGetValue(p, out replacement)) p = replacement;
|
||||
return base.VisitParameter(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
63
CoreCms.Net.Model/Entities/Expression/PredicateBuilder.cs
Normal file
63
CoreCms.Net.Model/Entities/Expression/PredicateBuilder.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms
|
||||
* ProjectName: 核心内容管理系统
|
||||
* Web: https://www.corecms.net
|
||||
* Author: 大灰灰
|
||||
* Email: jianweie@163.com
|
||||
* CreateTime: 2021/1/31 21:45:10
|
||||
* Description: 暂无
|
||||
***********************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace CoreCms.Net.Model.Entities.Expression
|
||||
{
|
||||
public static class PredicateBuilder
|
||||
{
|
||||
public static Expression<Func<T, bool>> True<T>()
|
||||
{
|
||||
return f => true;
|
||||
}
|
||||
|
||||
public static Expression<Func<T, bool>> False<T>()
|
||||
{
|
||||
return f => false;
|
||||
}
|
||||
|
||||
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second,
|
||||
Func<System.Linq.Expressions.Expression, System.Linq.Expressions.Expression,
|
||||
System.Linq.Expressions.Expression> merge)
|
||||
{
|
||||
// build parameter map (from parameters of second to parameters of first)
|
||||
var map = first.Parameters.Select((f, i) => new {f, s = second.Parameters[i]})
|
||||
.ToDictionary(p => p.s, p => p.f);
|
||||
|
||||
// replace parameters in the second lambda expression with parameters from the first
|
||||
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
|
||||
|
||||
// apply composition of lambda expression bodies to parameters from the first expression
|
||||
return System.Linq.Expressions.Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展啦
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="first"></param>
|
||||
/// <param name="second"></param>
|
||||
/// <returns></returns>
|
||||
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first,
|
||||
Expression<Func<T, bool>> second)
|
||||
{
|
||||
return first.Compose(second, System.Linq.Expressions.Expression.AndAlso);
|
||||
}
|
||||
|
||||
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first,
|
||||
Expression<Func<T, bool>> second)
|
||||
{
|
||||
return first.Compose(second, System.Linq.Expressions.Expression.OrElse);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user