using IWareDataAccess.Helper;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Linq.Expressions;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IWareDataAccess.Helper
|
{
|
public static class PredicateBuilder
|
{
|
private const String str_Start = "Start";
|
private const String str_End = "End";
|
|
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<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
|
Expression<Func<T, bool>> expr2)
|
{
|
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
|
return Expression.Lambda<Func<T, bool>>
|
(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
|
}
|
|
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
|
Expression<Func<T, bool>> expr2)
|
{
|
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
|
return Expression.Lambda<Func<T, bool>>
|
(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
|
|
|
}
|
|
|
/// <summary>
|
/// 获取筛选条件的查询表达式
|
/// </summary>
|
/// <typeparam name="T">查询对象</typeparam>
|
/// <typeparam name="T2">返回对象</typeparam>
|
/// <param name="t"></param>
|
/// <returns></returns>
|
public static Expression<Func<T2, bool>> GetFilterExpression<T, T2>(T t)
|
{
|
var f = PredicateBuilder.True<T2>();
|
//便利所有属性
|
Type type = t.GetType();
|
foreach (var i in type.GetProperties())
|
{
|
var _noDbFilterAttribute = AttributeHelper.GetNoDbFilterAttributeByPro(i);
|
if (_noDbFilterAttribute.Length > 0)
|
{
|
continue;
|
}
|
object v = Helper.GetFieldValueByName(t, i.Name);
|
if (i.PropertyType == typeof(String))
|
{
|
string value;
|
if (v != null)
|
{
|
value = v.ToString();
|
f = f.And(x => Helper.GetFieldValueByName(x, i.Name) != null ? Helper.GetFieldValueByName(x, i.Name).ToString() == value : false);
|
}
|
}
|
else if (i.PropertyType == typeof(DateTime?))
|
{
|
if (v != null)
|
{
|
DateTime value = DateTime.Parse(v.ToString());
|
|
if (i.Name.IndexOf(str_Start) > -1)
|
{
|
var name = GetTimeName(i.Name, str_Start);
|
//f = f.And(x => x.updateTime > value);
|
f = f.And(x => Helper.GetFieldValueByName(x, name) != null ? Convert.ToDateTime(Helper.GetFieldValueByName(x, name)) > value : false);
|
}
|
if (i.Name.IndexOf(str_End) > -1)
|
{
|
var name = GetTimeName(i.Name, str_End);
|
//f = f.And(x => x.updateTime < value);
|
f = f.And(x => Helper.GetFieldValueByName(x, name) != null ? Convert.ToDateTime(Helper.GetFieldValueByName(x, name)) < value : false);
|
}
|
}
|
}
|
else
|
{
|
if (v != null)
|
{
|
f = f.And(x => Object.Equals(Helper.GetFieldValueByName(x, i.Name), v));
|
}
|
}
|
}
|
return f;
|
}
|
|
|
|
/// <summary>
|
/// 解析前台查询传过来的属性名
|
/// </summary>
|
/// <param name="inputName"></param>
|
/// <returns></returns>
|
private static string GetTimeName(string inputName, string indexName)
|
{
|
//updateTimeStart updateTimeEnd
|
//doTimeStart doTimeEnd createTimeEnd
|
//createTimeStart
|
|
int index = 0;
|
|
index = inputName.IndexOf(indexName);
|
if (index > -1)
|
{
|
return inputName.Substring(0, index);
|
}
|
return inputName;
|
}
|
}
|
}
|