同步操作将从 李玉宝/OpenAuth.Core 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// ***********************************************************************// Assembly : FairUtility// Author : Yubao Li// Created : 08-18-2015//// Last Modified By : Yubao Li// Last Modified On : 08-18-2015// ***********************************************************************// <copyright file="DynamicLinq.cs" company="">// Copyright (c) . All rights reserved.// </copyright>// <summary>动态linq</summary>// ***********************************************************************using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Reflection;namespace Infrastructure{public static class DynamicLinq{public static ParameterExpression CreateLambdaParam<T>(string name){return Expression.Parameter(typeof(T), name);}/// <summary>/// 创建linq表达示的body部分/// </summary>public static Expression GenerateBody<T>(this ParameterExpression param, Filter filterObj){PropertyInfo property = typeof(T).GetProperty(filterObj.Key);//组装左边Expression left = Expression.Property(param, property);//组装右边Expression right = null;if (property.PropertyType == typeof(int)){right = Expression.Constant(int.Parse(filterObj.Value));}else if (property.PropertyType == typeof(DateTime)){right = Expression.Constant(DateTime.Parse(filterObj.Value));}else if (property.PropertyType == typeof(string)){right = Expression.Constant((filterObj.Value));}else if (property.PropertyType == typeof(decimal)){right = Expression.Constant(decimal.Parse(filterObj.Value));}else if (property.PropertyType == typeof(Guid)){right = Expression.Constant(Guid.Parse(filterObj.Value));}else if (property.PropertyType == typeof(bool)){right = Expression.Constant(filterObj.Value.Equals("1"));}else if (property.PropertyType == typeof(Guid?)){left = Expression.Property(left, "Value");right = Expression.Constant(Guid.Parse(filterObj.Value));}else{throw new Exception("暂不能解析该Key的类型");}//c.XXX=="XXX"Expression filter = Expression.Equal(left, right);switch (filterObj.Contrast){case "<=":filter = Expression.LessThanOrEqual(left, right);break;case "<":filter = Expression.LessThan(left, right);break;case ">":filter = Expression.GreaterThan(left, right);break;case ">=":filter = Expression.GreaterThanOrEqual(left, right);break;case "!=":filter = Expression.NotEqual(left, right);break;case "like":filter = Expression.Call(left, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),Expression.Constant(filterObj.Value));break;case "not in":var listExpression = Expression.Constant(filterObj.Value.Split(',').ToList()); //数组var method = typeof(List<string>).GetMethod("Contains", new Type[] { typeof(string) }); //Contains语句filter = Expression.Not(Expression.Call(listExpression, method, left));break;case "in":var lExp = Expression.Constant(filterObj.Value.Split(',').ToList()); //数组var methodInfo = typeof(List<string>).GetMethod("Contains", new Type[] { typeof(string) }); //Contains语句filter = Expression.Call(lExp, methodInfo, left);break;}return filter;}public static Expression<Func<T, bool>> GenerateTypeBody<T>(this ParameterExpression param, Filter filterObj){return (Expression<Func<T, bool>>)(param.GenerateBody<T>(filterObj));}/// <summary>/// 创建完整的lambda/// </summary>public static LambdaExpression GenerateLambda(this ParameterExpression param, Expression body){//c=>c.XXX=="XXX"return Expression.Lambda(body, param);}public static Expression<Func<T, bool>> GenerateTypeLambda<T>(this ParameterExpression param, Expression body){return (Expression<Func<T, bool>>)(param.GenerateLambda(body));}public static Expression AndAlso(this Expression expression, Expression expressionRight){return Expression.AndAlso(expression, expressionRight);}public static Expression Or(this Expression expression, Expression expressionRight){return Expression.Or(expression, expressionRight);}public static Expression And(this Expression expression, Expression expressionRight){return Expression.And(expression, expressionRight);}//系统已经有该函数的实现//public static IQueryable<T> Where<T>(this IQueryable<T> query, Expression expression)//{// Expression expr = Expression.Call(typeof(Queryable), "Where", new[] { typeof(T) },// Expression.Constant(query), expression);// //生成动态查询// IQueryable<T> result = query.Provider.CreateQuery<T>(expr);// return result;//}public static IQueryable<T> GenerateFilter<T>(this IQueryable<T> query, string filterjson){if (!string.IsNullOrEmpty(filterjson)){var filters = JsonHelper.Instance.Deserialize<IEnumerable<Filter>>(filterjson);var param = CreateLambdaParam<T>("c");Expression result = Expression.Constant(true);foreach (var filter in filters){result = result.AndAlso(param.GenerateBody<T>(filter));}query = query.Where(param.GenerateTypeLambda<T>(result));}return query;}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。