C#动态拼接Linq
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace DynamicLinqDemo { public class Person { public string Name { get; set; } } /// <summary> /// 动态拼接Linq /// https://www.cnblogs.com/LifeDecidesHappiness/p/15270834.html /// LDH @ 2021-9-15 /// </summary> internal class Program { private static void Main() { Console.Title = "动态拼接Linq"; DynamicLinq(); Console.ReadKey(); } /// <summary> /// 动态拼接Linq /// </summary> private static void DynamicLinq() { var listPeople = new List<Person>(); var p = new Person { Name = "LDH" }; listPeople.Add(p); Console.WriteLine("Person.Name = " + p.Name); PrintLine(); var filterCondition = GetExpression<Person>("x", "Contains", "Name", "LD"); Console.WriteLine("Contains 动态 Linq:" + filterCondition); //x => x.Name.Contains("LD") var expression = listPeople.Where(filterCondition.Compile()); Console.WriteLine("Contains Count:" + expression.Count()); PrintLine(); var filterCondition2 = GetExpression<Person>("x", "StartsWith", "Name", "LD"); Console.WriteLine("StartsWith 动态 Linq:" + filterCondition2); //x => x.Name.StartsWith("LD") var expression2 = listPeople.Where(filterCondition2.Compile()); Console.WriteLine("StartsWith Count:" + expression2.Count()); PrintLine(); var filterCondition3 = GetExpression<Person>("x", "EndsWith", "Name", "LD"); Console.WriteLine("EndsWith 动态 Linq:" + filterCondition3); //x => x.Name.EndsWith("LD") var expression3 = listPeople.Where(filterCondition3.Compile()); Console.WriteLine("EndsWith Count:" + expression3.Count()); PrintLine(); var filterCondition4 = GetExpression<Person>("x", "Equals", "Name", "LDH"); Console.WriteLine("Equals 动态 Linq:" + filterCondition4); //x => x.Name.Equals("LDH") var expression4 = listPeople.Where(filterCondition4.Compile()); Console.WriteLine("Equals Count:" + expression4.Count()); PrintLine(); } /// <summary> /// 动态拼接Linq /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lambdaVariableName">Lambda变量名(eg: x,m,n,type)</param> /// <param name="operatorName">StartsWith,Contains,EndsWith</param> /// <param name="propertyName">属性名称</param> /// <param name="propertyValue">属性值</param> /// <returns>eg:x => x.Name.Contains("LD")</returns> private static Expression<Func<T, bool>> GetExpression<T>(string lambdaVariableName, string operatorName, string propertyName, string propertyValue) { var parameterExp = Expression.Parameter(typeof(T), lambdaVariableName); var propertyExp = Expression.Property(parameterExp, propertyName); var method = typeof(string).GetMethod(operatorName, new[] { typeof(string) }); var someValue = Expression.Constant(propertyValue, typeof(string)); var containsMethodExp = Expression.Call(propertyExp, method ?? throw new InvalidOperationException(), someValue); return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp); } /// <summary> /// 分割线 /// </summary> private static void PrintLine() { Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"); } } }
/// <summary> /// 动态拼接Linq (属性值为 string 类型) /// var myFilter = GetExpressionPropertyValueString<T>("x", "Contains", fieldName, fieldValue); /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lambdaVariableName">Lambda变量名(eg: x,m,n,type)</param> /// <param name="operatorName">Equals,StartsWith,Contains,EndsWith</param> /// <param name="propertyName">属性名称</param> /// <param name="propertyValue">属性值(string类型)</param> /// <returns>eg:x => x.Name.Contains("LD")</returns> private static Expression<Func<T, bool>> GetExpressionPropertyValueString<T>(string lambdaVariableName, string operatorName, string propertyName, string propertyValue) { var parameterExp = Expression.Parameter(typeof(T), lambdaVariableName); var propertyExp = Expression.Property(parameterExp, propertyName); var method = typeof(string).GetMethod(operatorName, new[] {typeof(string)}); var someValue = Expression.Constant(propertyValue, typeof(string)); var containsMethodExp = Expression.Call(propertyExp, method ?? throw new InvalidOperationException(), someValue); return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp); } /// <summary> /// 动态拼接Linq (属性值为 int 类型) /// var whereExpression = GetExpressionPropertyValueInt<T>("x", "Equals", fieldName, Convert.ToInt32(fieldValue)); /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lambdaVariableName">Lambda变量名(eg: x,m,n,type)</param> /// <param name="operatorName">Equals</param> /// <param name="propertyName">属性名称</param> /// <param name="propertyValue">属性值(int类型)</param> /// <returns>eg:x => x.Age.Equals(18)</returns> private static Expression<Func<T, bool>> GetExpressionPropertyValueInt<T>(string lambdaVariableName, string operatorName, string propertyName, int propertyValue) { var parameterExp = Expression.Parameter(typeof(T), lambdaVariableName); var propertyExp = Expression.Property(parameterExp, propertyName); var method = typeof(int).GetMethod(operatorName, new[] { typeof(int) }); var someValue = Expression.Constant(propertyValue, typeof(int)); var containsMethodExp = Expression.Call(propertyExp, method ?? throw new InvalidOperationException(), someValue); return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp); }
本文作者:Love In Winter
本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/15270834.html
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/15270834.html
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
![扫一扫,支付宝打赏](https://files.cnblogs.com/files/LifeDecidesHappiness/%E6%94%AF%E4%BB%98%E5%AE%9D.bmp)
![扫一扫,微信打赏](https://files.cnblogs.com/files/LifeDecidesHappiness/%E5%BE%AE%E4%BF%A1%E6%94%B6%E6%AC%BE%E7%A0%81.bmp)