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("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"); } } }